Skip to main content
 首页 » 编程设计

python之pypdf python工具

2025年05月04日12duanxz

使用 pypdf python 模块如何读取以下 pdf 文件 http://www.envis-icpe.com/pointcounterpointbook/Hindi_Book.pdf

# -*- coding: utf-8 -*- 
from pyPdf import PdfFileWriter, PdfFileReader 
import pyPdf 
 
def getPDFContent(path): 
   content = "" 
   # Load PDF into pyPDF 
   pdf = pyPdf.PdfFileReader(file(path, "rb")) 
   # Iterate pages 
   for i in range(0, pdf.getNumPages()): 
      # Extract text from page and add to content 
      content += pdf.getPage(i).extractText() + "\n" 
   # Collapse whitespace 
   content = " ".join(content.replace(u"\xa0", " ").strip().split()) 
   return content 
 
print getPDFContent("/home/tom/Desktop/Hindi_Book.pdf").encode("ascii", "xmlcharrefreplace") 

以上只打印二进制

以及如何打印下面代码中的内容

from pyPdf import PdfFileWriter, PdfFileReader 
import sys 
import pyPdf 
 
from pyPdf import PdfFileWriter, PdfFileReader 
 
output = PdfFileWriter() 
input1 = PdfFileReader(file("/home/tom/Desktop/Hindi_Book.pdf", "rb")) 
 
# print the title of document1.pdf 
print "title = %s" % (input1.getDocumentInfo().title) 

请您参考如下方法:

请注意,您引用的 pdf 文档的大部分“文本”根本不是真正的文本:它主要是图像。当我尝试时,实际文本似乎被正确提取了(尽管我必须承认,除了首页和页码上的一些片段外,我无法阅读它;-))。

关于第二个问题:我不太清楚你问的是什么。