[英]How to use PyPDF2 in a script?
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,但我不确定如何在这样的脚本中使用它。
编辑:我成功地导入了操作系统和系统。
import os
import sys
有多个问题:
from PyDF2 import ...
:一个错字。 你的意思是PyPDF2
而不是PyDF2
PdfFileWriter
已导入,但从未使用过(旁注:它是最新版本的 PyPDF2 中的 PdfReader 和 PdfWriter)with open("sample.pdf", "w") as f:'
: 语法错误for page in pdf.pages
写吗?DocumentInformation.author
是错误的。 我猜你的意思是pdf.metadata.author
txt
变量-我不明白为什么在重新分配它之前不使用它。也许这就是你想要的:
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 环境。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.