Python PIL由于某些原因无法打开PDF [英] Python PIL can't open PDFs for some reason

查看:261
本文介绍了Python PIL由于某些原因无法打开PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的程序能够打开PNG,而不能打开PDF,因此我只是为了测试而已,即使是简单的PDF也无法打开.而且我不知道为什么.

So my program is able to open PNGs but not PDFs, so I made this just to test, and it still isn't able to open even a simple PDF. And I don't know why.

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

After trying PyPDF2 as suggested (Thanks for the link by the way), I am getting this error with my code. import 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]

推荐答案

@Kevin指出(请参见下面的评论),PIL支持

As pointed out by @Kevin (see comment below) PIL has support for writing pdfs but not reading them.

要阅读pdf,您将需要一些其他库.您可以在此处,这是使用PyPDF2处理PDF的教程.

To read a pdf you will need some other library. You can look here which is a tutorial for handling PDFs with PyPDF2.

https://pythonhosted.org/PyPDF2/?utm_source=recordnotfound.com

这篇关于Python PIL由于某些原因无法打开PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆