python PDF转图片,World转PDF

 

 

 

 1 import tkinter
 2 from tkinter import filedialog
 3 from tkinter import messagebox
 4 import fitz,os
 5 from win32com.client import constants, gencache
 6 class dark:
 7     def __init__(self):
 8         self.init_window = tkinter.Tk()
 9         self.init_window.title('PDF转换工具')
10         self.init_window.geometry('500x100+10+10')
11         self.init_window.iconbitmap('ghost.ico')
12     def Button(self,row,column,text,func):
13         button = tkinter.Button(self.init_window,text=text,command=func)
14         button.grid(row=row,column=column,padx=66,pady=33)
15     def run(self):
16         self.Button(1,1,'PDF转图片',lambda: self.conver('PdfToImg'))
17         self.Button(1,2,'World转PDF',lambda: self.conver('WorldToPdf'))
18  
19         # self.lab = tkinter.Label(text='ok')
20         # self.lab.place(rely=1.0, relx=1.0, x=0, y=0, anchor='se')
21         self.init_window.mainloop()
22     def conver(self,type):
23         selectFile = filedialog.askopenfilename()
24         obj = Change(selectFile)
25         res = getattr(obj, type)
26         self.message(res())
27     def message(self,text):
28         messagebox.showinfo('结果',text)
29 class Change:
30     def __init__(self,filename):
31         self.filename = filename
32     def PdfToImg(self):
33         try:
34             self.doc = fitz.open(self.filename)
35             self.pageCount=self.doc.pageCount
36             for pg in range(self.pageCount):
37                 page = self.doc[pg]
38                 rotate = int(0)
39                 zoom_x = 2.0
40                 zoom_y = 2.0
41                 trans = fitz.Matrix(zoom_x, zoom_y).prerotate(rotate)
42                 pm = page.get_pixmap(matrix=trans, alpha=False)
43                 pm.save(self.filename+'%s.png' %pg)
44             return '转换完成'
45         except Exception as e:
46             return e
47     def WorldToPdf(self):
48         pdfname = self.filename+'.pdf'
49         word = gencache.EnsureDispatch('Word.Application')
50         doc = word.Documents.Open(self.filename, ReadOnly=1)
51         doc.ExportAsFixedFormat(pdfname,
52                                 constants.wdExportFormatPDF,
53                                 Item=constants.wdExportDocumentWithMarkup,
54                                 CreateBookmarks=constants.wdExportCreateHeadingBookmarks)
55         word.Quit(constants.wdDoNotSaveChanges)
56         return '转换完成'
57 if __name__ == '__main__':
58     obj = dark()
59     obj.run()

 

上一篇:android intent打开各种格式文档方法,android实战


下一篇:MarkDown学习