python PDF转图片,World转PDF
2022/2/9 20:18:50
本文主要是介绍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()
这篇关于python PDF转图片,World转PDF的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-03用FastAPI掌握Python异步IO:轻松实现高并发网络请求处理
- 2025-01-02封装学习:Python面向对象编程基础教程
- 2024-12-28Python编程基础教程
- 2024-12-27Python编程入门指南
- 2024-12-27Python编程基础
- 2024-12-27Python编程基础教程
- 2024-12-27Python编程基础指南
- 2024-12-24Python编程入门指南
- 2024-12-24Python编程基础入门
- 2024-12-24Python编程基础:变量与数据类型