1.代码
import os
import fitz
pdf_dir = []
def get_file():
docunames = os.listdir()
for docuname in docunames:
if os.path.splitext(docuname)[1] == '.pdf': # 目录下包含.pdf的文件
pdf_dir.append(docuname)
def conver_img():
for pdf in pdf_dir:
doc = fitz.open(pdf)
pdf_name = os.path.splitext(pdf)[0]
for pg in range(doc.page_count):
page = doc[pg]
rotate = int(0)
# 每个尺寸的缩放系数为2,这将为我们生成分辨率提高四倍的图像。
zoom_x = 3.0
zoom_y = 3.0
trans = fitz.Matrix(zoom_x, zoom_y).prerotate(rotate)
pm = page.get_pixmap(matrix=trans, alpha=False)
pm.save('%s.png' % pg)
if __name__ == '__main__':
get_file()
conver_img()
2,运行环境
编译器:2023pycharm专业版
python3.9
3遇到的问题
ModuleNotFoundError: No module named 'fitz'
解决办法:在pycharm终端安装缺少的库
pip install fitz
在执行发现 ModuleNotFoundError: No module named 'frontend'
解决办法
pip install PymuPDF