批量的PDF转图片的脚本python
import fitz
import os
def mkdir(path):
folder = os.path.exists(path)
if not folder: # 判断是否存在文件夹如果不存在则创建为文件夹
os.makedirs(path) # makedirs 创建文件时如果路径不存在会创建这个路径
else:
pass
def pdf_image(pdfPath, imgPath, zoom_x, zoom_y, rotation_angle):
"""
:param pdfPath: pdf文件的路径
:param imgPath: 图像要保存的文件夹
:return: None
"""
# 打开PDF文件
pdf = fitz.open(pdfPath)
name = pdf.name
name = name.replace('C:\Learning\program\\12.1\pdfs/', '').replace('.pdf', '')
# 逐页读取PDF
for pg in range(0, pdf.pageCount):
page = pdf[pg]
# 设置缩放和旋转系数
trans = fitz.Matrix(zoom_x, zoom_y).preRotate(rotation_angle)
pm = page.getPixmap(matrix=trans, alpha=False)
# 开始写图像
mkdir(imgPath)
pm.writePNG(imgPath+str(name)+"_"+str(pg) + ".png")
pdf.close()
file_dir = r'C:\\Learning\\program\\12.1\\pdfs\\'
file_list = []
for items in os.walk(file_dir, topdown=False):
file_list = items[2]
for file in file_list:
head = 'C:\\Learning\\program\\12.1\\pdfs/'
pdf_image(head + file, r"C:\\Learning\\program\\12.1\\images/", 5, 5, 0)