所以我的程序能够打开PNG而不是PDF,所以我这样做只是为了测试,它仍然无法打开一个简单的PDF . 我不知道为什么 .
from PIL import Image
with Image.open(r"Adams, K\a.pdf") as file:
print file
Traceback (most recent call last):
File "C:\Users\Hayden\Desktop\Scans\test4.py", line 3, in <module>
with Image.open(r"Adams, K\a.pdf") as file:
File "C:\Python27\lib\site-packages\PIL\Image.py", line 2590, in open
% (filename if filename else fp))
IOError: cannot identify image file 'Adams, K\\a.pdf'
按照建议尝试PyPDF2后(感谢链接),我的代码出错了 . 导入PyPDF2
pdf_file= open(r"Adams, K (6).pdf", "rb")
read_pdf= PyPDF2.PdfFileReader(pdf_file)
number_of_pages = read_pdf.getNumPages()
print number_of_pages
Xref table not zero-indexed. ID numbers for objects will be corrected. [pdf.py:1736]
1 回答
正如@Kevin所指出的那样(见下面的评论)PIL支持writing pdfs but not reading them .
要阅读pdf,您需要一些其他库 . 您可以查看here这是使用PyPDF2处理PDF的教程 .
https://pythonhosted.org/PyPDF2/?utm_source=recordnotfound.com