python PDF转图片,World转PDF

Python3.9

—————pip3 install  PyMuPdf

—————pip3 install PyWin32

import tkinter
from tkinter import filedialog
from tkinter import messagebox
import fitz,os
from win32com.client import constants, gencache
class dark:
    def __init__(self):
        self.init_window = tkinter.Tk()
        self.init_window.title('PDF转换工具')
        self.init_window.geometry('500x100+10+10')
        self.init_window.iconbitmap('ghost.ico')
    def Button(self,row,column,text,func):
        button = tkinter.Button(self.init_window,text=text,command=func)
        button.grid(row=row,column=column,padx=66,pady=33)
    def run(self):
        self.Button(1,1,'PDF转图片',lambda: self.conver('PdfToImg'))
        self.Button(1,2,'World转PDF',lambda: self.conver('WorldToPdf'))

        # self.lab = tkinter.Label(text='ok')
        # self.lab.place(rely=1.0, relx=1.0, x=0, y=0, anchor='se')
        self.init_window.mainloop()
    def conver(self,type):
        selectFile = filedialog.askopenfilename()
        obj = Change(selectFile)
        res = getattr(obj, type)
        self.message(res())
    def message(self,text):
        messagebox.showinfo('结果',text)
class Change:
    def __init__(self,filename):
        self.filename = filename
    def PdfToImg(self):
        try:
            self.doc = fitz.open(self.filename)
            self.pageCount=self.doc.pageCount
            for pg in range(self.pageCount):
                page = self.doc[pg]
                rotate = int(0)
                zoom_x = 2.0
                zoom_y = 2.0
                trans = fitz.Matrix(zoom_x, zoom_y).prerotate(rotate)
                pm = page.get_pixmap(matrix=trans, alpha=False)
                pm.save(self.filename+'%s.png' %pg)
            return '转换完成'
        except Exception as e:
            return e
    def WorldToPdf(self):
        pdfname = self.filename+'.pdf'
        word = gencache.EnsureDispatch('Word.Application')
        doc = word.Documents.Open(self.filename, ReadOnly=1)
        doc.ExportAsFixedFormat(pdfname,
                                constants.wdExportFormatPDF,
                                Item=constants.wdExportDocumentWithMarkup,
                                CreateBookmarks=constants.wdExportCreateHeadingBookmarks)
        word.Quit(constants.wdDoNotSaveChanges)
        return '转换完成'
if __name__ == '__main__':
    obj = dark()
    obj.run()

Leave a Reply

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

X