|
- import fitz
- import os
- name = []
- path = []
- i = 1
- url = r'F:\41-45\42-45\42' #定义存放PDF文件的路径
- #在路径内搜索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
- lujing=lujing.replace('.pdf','')
- if not os.path.exists(lujing):
- os.mkdir(lujing)
- print(name)
- # print(path)
- url = path
- # 打开PDF文件,生成一个对象
- doc = fitz.open(url)
- print(f'一共{doc.pageCount}页')
- pdf_name = os.path.splitext(url)[0]
- for pg in range(doc.pageCount):
- page = doc[pg]
- rotate = int(0)
- # 每个尺寸的缩放系数为2,这将为我们生成分辨率提高四倍的图像。
- zoom_x = 4.0
- zoom_y = 4.0
- trans = fitz.Matrix(zoom_x, zoom_y).preRotate(rotate)
- pm = page.getPixmap(matrix=trans, alpha=False)
- pm.writePNG(lujing +'\\'+ f'{i}.png')
- i = i+1
- print(f'第{i}页转换完成')
- i = 1
复制代码
|
|