|
- import fitz
- import os
- def pdf2png(url):
- name = []
- path = []
- i = 1
- #在路径内搜索PDF文件,并加入列表,生成字典,键值对分别是文件名和文件路径
- for lujing,liebiao,wenjian in os.walk(url):
- for a in wenjian:
- if a.endswith('pdf'):
- patha = lujing +'\\' +a
- path.append(patha)
- name.append(a)
- zidian = dict(zip(name,path))
- #在字典内拿到文件名创建文件夹,然后转图片放到对应文件夹
- for name,path in zidian.items():
- lujing=path.replace('.pdf','')
- if not os.path.exists(lujing):
- os.mkdir(lujing)
- # 打开PDF文件,生成一个对象
- doc = fitz.open(path)
- print(path,f'一共{doc.pageCount}页')
- for pg in range(doc.pageCount):
- page = doc[pg]
- rotate = int(0)
- # 每个尺寸的缩放系数为2,这将为我们生成分辨率提高两倍的图像。
- zoom_x = 2.0
- zoom_y = 2.0
- trans = fitz.Matrix(zoom_x, zoom_y).preRotate(rotate)
- pm = page.getPixmap(matrix=trans, alpha=False)
- pm.writePNG(lujing +'\\'+ '{:02}.png'.format(i))
- i = i+1
- print(f'第{i-1}页转换完成')
- i = 1
- url = input('请输入文件夹地址:')
- pdf2png(url)
复制代码
|
|