透過 Python 達成一般文書動作(word excel pdf printer)

前言 因為有案子的需求要使用自動化的文書動作,因此這篇 blog 來記錄一下我用了哪些工具來達到這些功能,內容蠻雜的。 Pdf 判斷 pdf 是否可被開啟(是否沒損毀) https://pypi.org/project/PyPDF2/ from PyPDF2 import PdfWriter, PdfReader def check_pdf_vaild(self): try: PdfReader(self.filename) return True except: return False pdf 轉 png https://pypi.org/project/pdf2image/ 此套件為 poppler 的封裝,請安裝 poppler。 from pdf2image import convert_from_path images = convert_from_path(f'{file_path}.pdf') images[0].save(f"{file_path}.png") 此 images 為 pillow 物件,因此如果同時要剪裁,可以有以下操作 from pdf2image import convert_from_path images = convert_from_path(f'{file_path}.pdf') images[0].crop((300, 50, images[0].size[0]/2, images[0].size[1]/2.55)).save(f"{file_path}.png") pdf 剪裁 https://pypi.org/project/PyPDF2/ from PyPDF2 import PdfWriter, PdfReader reader = PdfReader(f'{file_path}.pdf') writer = PdfWriter() page = reader....

July 20, 2022 · 2 min · Vincent55