import fitz
import os
def pyMuPDF_fitz(pdfPath, imagePath, num, all=True):
"""
:param pdfPath: pdf文件路径
:param imagePath: 图片存放目录
:param num: 第几页,索引从0开始
:param all: 是否转化所有页面
:return:
"""
pdfDoc = fitz.open(pdfPath)
if all:
start = 0
end = pdfDoc.pageCount
else:
start = num
end = num+1
for i in range(start,end):
page = pdfDoc[i]
rotate = int(0)
zoom_xy = (1.3333,1.3333)
mat = fitz.Matrix(zoom_xy[0],zoom_xy[1]).prerotate(rotate)
pix = page.getPixmap(matrix=mat,alpha=False)
if not os.path.exists(imagePath):
os.makedirs(imagePath)
pix.writePNG(f"{imagePath}{i}.png")
pdfPath = 'E://a.pdf'
imagePath = 'E://'
pyMuPDF_fitz(pdfPath, imagePath ,0,False)