ほとんどの PDF ファイルは読み取り専用ファイルであるため、編集のニーズを満たすために、PDF ファイルを直接 Word ファイルに変換して操作できる場合があります。
インターネットでPDFファイルをWordに変換するPythonの関連記事を読んだ後、より複雑に感じ、一部のチャートの使用には特別な処理が必要です。
この記事では主に、Python を使用して PDF を Word に変換するビジネス プロセスを実現する方法について説明しますが、今回は GUI アプリケーションは使用しません。
バージョンが競合する可能性があるため、開発プロセスで使用する必要がある Python 非標準ライブラリのバージョンをここにリストします。
python内核版本:3.6.8
PyMuPDF版本:1.18.17
pdf2docx版本:0.5.1
pip を選択して、使用される python 非標準ライブラリをインストールできます。
pip install PyMuPDF==1.18.17
pip 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.
素晴らしい過去
便宜上、Python 自動化に関連する 20 以上のモジュールを一気に記録しました。