使用Python将pdf第i页转为png图片

pip install pymupdf wxPython fitz

第一步,提取第i页为单独pdf

import fitz
import wx


class PDFExtractor(wx.Frame):
    def __init__(self, parent, title):
        super(PDFExtractor, self).__init__(parent, title=title, size=(400, 200))

        self.panel = wx.Panel(self)
        self.file_picker = wx.FilePickerCtrl(self.panel, style=wx.FLP_DEFAULT_STYLE | wx.FLP_USE_TEXTCTRL)
        self.start_page_input = wx.TextCtrl(self.panel)
        self.end_page_input = wx.TextCtrl(self.panel)
        self.extract_button = wx.Button(self.panel, label="Extract", size=(100, 30))

        self.extract_button.Bind(wx.EVT_BUTTON, self.extract_pages)

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.file_picker, 0, wx.EXPAND | wx.ALL, 10)
        self.sizer.Add(wx.StaticText(self.panel, label="Start Page:"), 0, wx.LEFT | wx.TOP, 10)
        self.sizer.Add(self.start_page_input, 0, wx.EXPAND | wx.ALL, 10)
        self.sizer.Add(wx.StaticText(self.panel, label="End Page:"), 0, wx.LEFT | wx.TOP, 10)
        self.sizer.Add(self.end_page_input, 0, wx.EXPAND | wx.ALL, 10)
        self.sizer.Add(self.extract_button, 0, wx.ALIGN_CENTER | wx.ALL, 10)

        self.panel.SetSizerAndFit(self.sizer)
        self.Show()

    def extract_pages(self, event):
        file_path = self.file_picker.GetPath()
        start_page = int(self.start_page_input.GetValue())
        end_page = int(self.end_page_input.GetValue())

        doc = fitz.open(file_path)
        output_doc = fitz.open()

        for page_num in range(start_page - 1, end_page):
            output_doc.insert_pdf(doc, from_page=page_num, to_page=page_num)

        output_path = file_path.replace(".pdf", "_extracted.pdf")
        output_doc.save(output_path)
        output_doc.close()
        doc.close()

        wx.MessageBox("Extraction complete!", "Success", wx.OK | wx.ICON_INFORMATION)


app = wx.App()
PDFExtractor(None, title="PDF Extractor")
app.MainLoop()

第二步,将单独pdf转为png

import fitz


def pdf2img(pdf_path, img_path):
    pdfDoc = fitz.open(pdf_path)
    for page in pdfDoc.pages():
        # 将页面转换为图片
        zoom_x = 6
        zoom_y = 6
        mat = fitz.Matrix(zoom_x, zoom_y)
        pix = page.get_pixmap(matrix=mat, dpi=None, colorspace='rgb', alpha=False)
        # 保存图片
        pix.save(img_path)


pdf2img("C:/Users/卡尔/Desktop/img/本科组(1)_extracted.pdf", 'C:/Users/卡尔/Desktop/img/计算机设计大赛省一等奖.png')

得到上图计算机设计大赛png图片

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值