pypdf2 未定义
pypdf2 is not defined
python 3.7 版本的新功能。
尝试使用 pypdf2
但我遇到一个无法自行修复的错误:
我的命令:
pdfFile2 = open(r"C:\Users\Luca\Desktop\python\tutorial\doc2.pdf", "wb") # w=write, r=read, b=binary
writer1 = pyPDF2.PdfFileWriter()
错误:
Traceback (most recent call last):
File "C:/Users/Luca/Desktop/python/tutorial/tutorial.py", line 8, in <module>
writer1 = pyPDF2.PdfFileWriter()
NameError: name 'pyPDF2' is not defined
我已经安装了 pypdf2
库,但我无法继续,我该如何解决这个问题?
这很可能是因为您的示例在代码的第二行中使用 pyPDF2
(小 p
)而不是 PyPDF2
(大写 P
)。
第一步:
PyPDF2 是一个纯 Python 包,因此您可以使用 pip 安装它(假设 pip 在您的系统路径中):
python -m pip install pypdf2
第 2 步:
安装该软件包后。
您可以从该库中导入特定的包,例如 PdfFileReader & PdfFileWriter。
from PyPDF2 import PdfFileReader, PdfFileWriter
第 3 步:
最后,您可以直接实例化该模块对象
# 对于 Reader
reader=PdfFileReader(open("fpath",'rb'))
# 写
writer=PdfFileWriter()
outfp=open("outpath",'wb')
writer.write(outfp)
python 3.7 版本的新功能。
尝试使用 pypdf2
但我遇到一个无法自行修复的错误:
我的命令:
pdfFile2 = open(r"C:\Users\Luca\Desktop\python\tutorial\doc2.pdf", "wb") # w=write, r=read, b=binary
writer1 = pyPDF2.PdfFileWriter()
错误:
Traceback (most recent call last):
File "C:/Users/Luca/Desktop/python/tutorial/tutorial.py", line 8, in <module>
writer1 = pyPDF2.PdfFileWriter()
NameError: name 'pyPDF2' is not defined
我已经安装了 pypdf2
库,但我无法继续,我该如何解决这个问题?
这很可能是因为您的示例在代码的第二行中使用 pyPDF2
(小 p
)而不是 PyPDF2
(大写 P
)。
第一步: PyPDF2 是一个纯 Python 包,因此您可以使用 pip 安装它(假设 pip 在您的系统路径中):
python -m pip install pypdf2
第 2 步: 安装该软件包后。 您可以从该库中导入特定的包,例如 PdfFileReader & PdfFileWriter。
from PyPDF2 import PdfFileReader, PdfFileWriter
第 3 步: 最后,您可以直接实例化该模块对象
# 对于 Reader
reader=PdfFileReader(open("fpath",'rb'))
# 写
writer=PdfFileWriter()
outfp=open("outpath",'wb')
writer.write(outfp)