Skip to main content
 首页 » 编程设计

python之pdf2image 如何使用 "enable all features"阅读 pdf之Windows

2025年05月04日14davidwang456

我有一个 pdf,我想用 Python 阅读它。当我使用 Acrobat 在我的机器上打开它时,我收到以下消息,当我单击“启用所有功能”时,该文件显示了它的实际内容。

当我尝试在 python 中读取它时,我怎样才能实现相同的操作,以便 python 读取实际文本而不读取下面的文本

“请稍候...如果此消息最终没有被文档的正确内容替换,您的 PDF 查看器可能无法显示此类文档。您可以升级到最新版本的 Adob​​e Reader for Windows ®、Mac 或 Linux®,请访问 http://www.adobe.com/go/reader_download。有关 Adob​​e Reader 的更多帮助,请访问 http://www.adobe.com/go/acrreader。Windows 是 Microsoft Corporation 在美国和/或其他国家/地区的注册商标或商标。Mac 是商标Apple Inc. 的商标,在美国和其他国家注册。Linux 是 Linus Torvalds 在美国和其他国家的注册商标。”

我的代码如下

from PIL import Image 
import pytesseract 
 
homepath = r'C:\Users\xxxx\\' 
 
 
files = "bbbb.pdf" 
PDFfilename = homepath  + files 
 
from pdf2image import convert_from_path 
pages = convert_from_path(PDFfilename, 500) 
 
i=1 
for page in pages: 
    page.save(homepath +'out'+str(i)+'.jpg', 'JPEG') 
    text = pytesseract.image_to_string(Image.open(homepath +'out'+str(i)+'.jpg')) 
    print(text) 
    i=i+1 

请您参考如下方法:

您看到的“请稍候...”页面是您的 pdf 中唯一实际的 pdf 样式内容(即具有内容流和资源等的 pdf 页面对象)。

启用所有功能后,您将看到 pdf 中包含的 XFA 表单的内容。

XFA (also known as XFA forms) stands for XML Forms Architecture, a family of proprietary XML specifications that was suggested and developed by JetForm to enhance the processing of web forms. It can be also used in PDF files starting with the PDF 1.5 specification. The XFA specification is referenced as an external specification necessary for full application of the ISO 32000-1 specification (PDF 1.7). The XML Forms Architecture was not standardized as an ISO standard, and has been deprecated in PDF 2.0.

( Wikipedia on XFA )

Most PDF processors do not handle XFA content.特别是大多数免费或开放的 pdf 库不这样做。

不过,只要您的 pdf 库允许直接访问低级 pdf 对象,您可以做的就是检索 XFA XML 并将其作为 XML 流进行分析。

它位于Catalog -> AcroForm -> XFA 对象中:

The XFA entry shall be either a stream containing the entire XFA resource or an array specifying individual packets that together make up the entire XFA resource. [...]

A packet is a pair of string and stream. The string contains the name of the XML element and the stream contains the complete text of the XML element.

(ISO 32000-1 第 12.7.8 节 XFA 表格)