import PyPDF2
from PyDF2 import PdfFileReader, PdfFileWriter
file_path="sample.pdf"
pdf = PdfFileReader(file_path)
with open("sample.pdf", "w") as f:'
for page_num in range(pdf.numPages):
pageObj = pdf.getPage(page_num)
try:
txt = pageObj.extractText()
txt = DocumentInformation.author
except:
pass
else:
f.write(txt)
f.close()收到错误: ModuleNotFoundError:没有名为“PyPDF2”的模块
编写我想要在PDF中扫描的第一个脚本,然后提取文本并将其写入txt文件。我试图使用pyPDF2,但我不知道如何在这样的脚本中使用它。
编辑:我成功地导入了这样的os & sys。
import os
import sys发布于 2022-06-01 20:26:42
有多个问题:
from PyDF2 import ...:一个错误。你指的是PyPDF2而不是PyDF2PdfFileWriter是导入的,但从未使用过(附带说明:它是最新版本的PyPDF2中的PdfReader和PdfWriter )with open("sample.pdf", "w") as f:':语法错误for page in pdf.pages吗?DocumentInformation.author错了。我猜你是说pdf.metadata.authortxt变量--我不明白为什么在重新分配之前不使用它。也许这就是你想要的
from PyPDF2 import PdfReader
def get_text(pdf_file_path: str) -> str:
text = ""
reader = PdfReader(pdf_file_path)
for page in reader.pages:
text += page.extract_text()
return text
text = get_text("example.pdf")
with open("example.txt", "w") as f:
f.write(text)安装问题
如果您有安装问题,也许安装PyPDF2的文档可以帮助您吗?
如果以python your_script_name.py的形式在控制台中执行脚本,则可能需要检查
python -c "import PyPDF2; print(PyPDF2.__version__)"这应该会显示您的PyPDF2版本。如果没有,您使用的Python环境没有安装PyPDF2。请注意,您的系统可能具有任意的许多Python环境。
https://stackoverflow.com/questions/72408533
复制相似问题