2

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)
4

2 に答える 2

1

参照する PDF ドキュメントの「テキスト」のほとんどは、実際のテキストではなく、ほとんどが画像であることに注意してください。試してみると、実際のテキストは正しく抽出されているようです (ただし、フロント ページの一部のスニペットとページ番号を除いて、それを読むことはできません ;-))。

2番目の質問については、あなたが何を求めているのかわかりません。

于 2010-10-04T14:07:21.120 に答える