【问题标题】:PyPDF2, how to fix their example code to conform with Python 3PyPDF2,如何修复他们的示例代码以符合 Python 3
【发布时间】:2015-03-13 22:22:23
【问题描述】:

我是 Python 新手 - 我使用的是 Python 3.4,但我能找到的大多数在线示例代码都是针对 Python 2 的。特别是,我尝试使用的包 PyPDF2 有示例代码: https://github.com/mstamy2/PyPDF2/blob/master/Sample_Code/basic_features.py 那是针对 Python 2 的。我似乎无法运行它。我修复了打印部分(第 7 行)以包括括号。我不知道如何解决的是第 44 行和第 45 行,您实际上是在其中保存了经过修改的 pdf。代码的相关部分是:

from PyPDF2 import PdfFileWriter, PdfFileReader

output = PdfFileWriter()
input1 = PdfFileReader(open("document1.pdf", "rb"))

output.addPage(input1.getPage(0))

outputStream = file("PyPDF2-output.pdf", "wb")
output.write(outputStream)

我的 Python 不理解 file()。给一个错误

outputStream = file("PyPDF2-output.pdf", "wb")

有什么建议吗?是否有我应该导入的库来运行它,或者 Python 2 和 3 之间的编写方式有什么不同?

【问题讨论】:

标签: python pdf


【解决方案1】:

Python 3 不再允许您使用 file 构造函数打开文件。请改用open

【讨论】:

  • 非常感谢,会试试看。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
  • 2020-03-11
  • 1970-01-01
  • 1970-01-01
  • 2017-04-20
相关资源
最近更新 更多