1. PDF 转 Word 简介
网上有许多在线 pdf 转 word 和 word 转 pdf 的网站,但其安全性未知,而且如果文件包含敏感信息,将文件上传至互联网上将是非常不安全的行为。
python 中的 pdf2docx 库允许您将 PDF 文件本地转换为可以在 Microsoft Word 中打开和编辑的 .docx文档;相应的, docx2pdf 库允许您将 Word 文件本地转换为 PDF 文件。
2. 安装 pdf2docx / docx2pdf 库
pip install pdf2docx
pip install docx2pdf
3. 文件格式转换 python 代码
以下是使用 Python 和库将 PDF 文件转换为 Word 文件的示例代码:
############## PDF 转 Word ##############
# Import the necessary libraries
from pdf2docx import parse
# Specify the path of the PDF file to be converted
pdf_path = "path/to/pdf_file.pdf"
# Specify the path and filename of the Word file to be created
docx_path = "path/to/docx_file.docx"
# Convert the PDF file to a Word file
parse(pdf_path, docx_path)
############## Word 转 PDF ##############
from docx2pdf import convert
# Specify the path of the PDF file to be converted
pdf_path = "path/to/pdf_file.pdf"
# Specify the path and filename of the Word file to be created
docx_path = "path/to/docx_file.docx"
# Convert the Word file to a PDF file
convert(docx_path, pdf_path)
使用时将 pdf_path
和 docx_path
换成自己文件的路径即可。
以上。