python如何使用最简单的方式将PDF转换成Word?
生活随笔
收集整理的這篇文章主要介紹了
python如何使用最简单的方式将PDF转换成Word?
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
由于PDF的文件大多都是只讀文件,有時候為了滿足可以編輯的需要通常可以將PDF文件直接轉換成Word文件進行操作。
看了網絡上面的python轉換PDF文件為Word的相關文章感覺都比較復雜,并且關于一些圖表的使用還要進行特殊的處理。
本篇文章主要講解關于如何使用python是實現將PDF轉換成Word的業務過程,這次沒有使用GUI應用的操作。
由于可能存在版本沖突的問題,這里將開發過程中需要使用的python非標準庫的版本列舉出來。
python內核版本:3.6.8 PyMuPDF版本:1.18.17 pdf2docx版本:0.5.1可以選擇pip的方式對使用到的python非標準庫進行安裝。
pip install PyMuPDF==1.18.17pip install pdf2docx==0.5.1完成上述的python依賴庫安裝以后,將pdf2docx導入到我們的代碼塊中。
# Importing the Converter class from the pdf2docx module. from pdf2docx import Converter然后,編寫業務函數的代碼塊,新建一個pdfToWord函數來處理轉換邏輯,主要就幾行代碼可以實現比較簡單。
def pdfToWord(pdf_file_path=None, word_file_path=None):"""It takes a pdf file path and a word file path as input, and converts the pdf file to a word file.:param pdf_file_path: The path to the PDF file you want to convert:param word_file_path: The path to the word file that you want to create"""# Creating a Converter object.converter_ = Converter(pdf_file_path)# The `convert` method takes the path to the word file that you want to create, and the start and end pages of the PDF# file that you want to convert.converter_.convert(word_file_path, start=0, end=None)converter_.close()最后,使用main函數調用pdfToWord函數可以直接完成文檔格式的轉換。
# A special variable in Python that evaluates to `True` if the module is being run directly by the Python interpreter, and # `False` if it has been imported by another module. if __name__ == '__main__':pdfToWord('D:/test-data-work/test_pdf.pdf', 'D:/test-data-work/test_pdf.docx')# Parsing Page 2: 2/5...Ignore Line "∑" due to overlap # Ignore Line "∑" due to overlap # Ignore Line "?" due to overlap # Ignore Line "A" due to overlap # Ignore Line "i =1" due to overlap # Ignore Line "?" due to overlap # Parsing Page 5: 5/5... # Creating Page 5: 5/5... # -------------------------------------------------- # Terminated in 3.2503201s.往期精彩
為了方便,我一口氣將20多個python自動化相關的模塊記錄了下來。
python最好用的能源類可視化圖表模塊,沒有之一!
python如何完成對 Excel文件的解密后讀取?
總結
以上是生活随笔為你收集整理的python如何使用最简单的方式将PDF转换成Word?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 简单计算器程序 (实现加减乘除基本运算)
- 下一篇: python决策树代码实现