该工具只能使用在英文目录下,且无法转换出图片,以及文本特殊格式。
下载依赖项
bash
pip install PyPDF2
升级依赖项
bash
pip install PyPDF2 --upgrade
查看库版本
bash
python -c "import PyPDF2; print(PyPDF2.__version__)"
下载第二个依赖项
bash
pip install python-docx
给权限
bash
pip install python-docx --user
验证
bash
python -c "from docx import Document; print('python-docx 安装成功')"
python
# -*- coding: utf-8 -*-
# pdf_to_word_pypdf2_python_docx.py
import PyPDF2
from docx import Document
def pdf_to_word_pypdf2_python_docx(pdf_path, word_path):
with open(pdf_path, 'rb') as pdf_file:
pdf_reader = PyPDF2.PdfReader(pdf_file) # 更新为PdfReader
document = Document()
for page in pdf_reader.pages: # 使用pages属性直接遍历
text = page.extract_text() # 更新方法名为extract_text()
document.add_paragraph(text)
document.save(word_path)
# 使用示例
pdf_to_word_pypdf2_python_docx('123456.pdf', 'output.docx')