pu369com

Python实现pdf转jpg

自己电脑没找到软件用,想到python

参考:https://zyicu.cn/?p=7656

代码:(还可以调整分辨率)

# -*- coding: utf-8 -*-

"""

1、安装库 pip install pymupdf

2、直接运行

"""

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()

 

补充:反过来,将jpg转PDF

参考https://pythonjishu.com/qmluqyxkazvkzuf/

import img2pdf
from PIL import Image

def image_to_pdf(image_filename, pdf_filename):
    with open(pdf_filename, "wb") as f:
        img = Image.open(image_filename)
        pdf_bytes = img2pdf.convert(img.filename)
        f.write(pdf_bytes)

  

 

posted on 2023-08-18 15:45  pu369com  阅读(244)  评论(0编辑  收藏  举报

导航