python将pdf按照图片保存

python将pdf按照图片保存

pdf_dir = []
chdir(Address) # 填文件地址
# 找pdf文件
docunames =listdir() # 读取该地址下的文件
if docunames == False:
    print("无文件")
for docuname in docunames: # 遍历文件,提取pdf文件
    if path.splitext(docuname)[1] == '.pdf' or path.splitext(docuname)[1] == '.PDF':  # 目录下包含.pdf的文件
        pdf_dir.append(docuname)
        
# 转换成图片
for pdf in pdf_dir:
    doc = fitz.open(pdf)
    print("此文件一共",doc.pageCount,"页...")
    pdf_name = path.splitext(pdf)[0]
    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(str(pg)+str(".png")) # 按页数命名
    print("完成全部.pdf到.png的转换!")

分享到 :
相关推荐

Leave a Reply

Your email address will not be published. Required fields are marked *