如何在脚本中使用PYPDF2?
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&类似的系统。
import os
import sys
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有多个问题:
来自PYDF2 Import ...
:错字。您的意思是pypdf2
而不是pydf2
pdffilewriter
已导入,但从未使用过(sidenote:pypdf2的PDFReader和pdfwriter,pypdf2的最新版本)使用open(“ sample.pdf”,“ w”)作为f:'
:缺少下一行的页面中的页面
?documentInformation.author
是错误的。我想您的意思是pdf.metadata.author
txt
变量 - 我不明白为什么在重新分配它之前不使用它。也许这就是您想要的:
安装问题,
以防万一您有安装问题,也许可以为您提供帮助?
如果您在控制台中执行脚本为
python yous_script_name.py
,则可能需要检查该输出的输出,应显示您的pypdf2版本。如果没有,则您正在使用的Python环境未安装PYPDF2。请注意,您的系统可能具有许多Python环境。
There are multiple issues:
from PyDF2 import ...
: A typo. You meantPyPDF2
instead ofPyDF2
PdfFileWriter
was imported, but never used (side-note: It's PdfReader and PdfWriter in the latest version of PyPDF2)with open("sample.pdf", "w") as f:'
: A syntax errorfor page in pdf.pages
?DocumentInformation.author
is wrong. I guess you meantpdf.metadata.author
txt
variable - I don't understand why you don't use it before you re-assign it.Maybe this is what you want:
Installation issues
In case you have installation issues, maybe the docs on installing PyPDF2 can help you?
If you execute your script in the console as
python your_script_name.py
you might want to check the output ofThat should show your PyPDF2 version. If it doesn't, it the Python environment you're using doesn't have PyPDF2 installed. Please note that your system might have arbitrary many Python environments.