2010-03-20 35 views
20

Tôi có một số mã để đọc từ tệp pdf. Có cách nào để đọc từng dòng từ tệp pdf (không phải trang) bằng Pypdf, Python 2.6, trên Windows không?Cách đọc từng dòng trong tệp pdf bằng PyPdf?

Đây là đoạn mã để đọc các trang pdf:

import pyPdf 

def getPDFContent(path): 
    content = "" 
    num_pages = 10 
    p = file(path, "rb") 
    pdf = pyPdf.PdfFileReader(p) 
    for i in range(0, num_pages): 
     content += pdf.getPage(i).extractText() + "\n" 
    content = " ".join(content.replace(u"\xa0", " ").strip().split()) 
    return content 

Cập nhật:

Mã gọi là thế này:

f= open('test.txt','w') 
pdfl = getPDFContent("test.pdf").encode("ascii", "ignore") 
f.write(pdfl) 
f.close() 

Trả lời

7

Hình như những gì bạn có là một lớn đoạn dữ liệu văn bản mà bạn muốn diễn giải từng dòng một.

Bạn có thể sử dụng lớp StringIO quấn rằng nội dung như một tập tin giống như đối tượng seekable:

>>> import StringIO 
>>> content = 'big\nugly\ncontents\nof\nmultiple\npdf files' 
>>> buf = StringIO.StringIO(content) 
>>> buf.readline() 
'big\n' 
>>> buf.readline() 
'ugly\n' 
>>> buf.readline() 
'contents\n' 
>>> buf.readline() 
'of\n' 
>>> buf.readline() 
'multiple\n' 
>>> buf.readline() 
'pdf files' 
>>> buf.seek(0) 
>>> buf.readline() 
'big\n' 

Trong trường hợp của bạn, làm:

from StringIO import StringIO 

# Read each line of the PDF 
pdfContent = StringIO(getPDFContent("test.pdf").encode("ascii", "ignore")) 
for line in pdfContent: 
    doSomething(line.strip()) 
+0

yea, nhưng mà tôi có thể nhưng điều này trong tôi mã ,, bởi vì tôi không thể làm cho nó hoạt động ??? –

+0

cùng một vấn đề, đây không phải là công việc nó cung cấp cho tôi toàn bộ trang ,, tôi chỉ muốn từng dòng :) –

+0

mã này đã không làm việc .... pdf.getPage (i) .extractText() nó nhận được dữ liệu rỗng –

5
import pyPdf 
def getPDFContent(path): 
    content = "" 
    num_pages = 10 
    p = file(path, "rb") 
    pdf = pyPdf.PdfFileReader(p) 
    for i in range(0, num_pages): 
     content += pdf.getPage(i).extractText() + "\n" 
    content = " ".join(content.replace(u"\xa0", " ").strip().split())  
    return content 
0

Sử dụng yieldPdfFileReader.pages thể đơn giản hóa mọi thứ,

from pyPdf import PdfFileReader 

def get_pdf_content_lines(pdf_file_path): 
    with open(pdf_file_path) as f: 
     pdf_reader = PdfFileReader(f) 
     for page in pdf_reader.pages: 
      for line in page.extractText().splitlines(): 
       yield line 

for line in get_pdf_content_lines('/path/to/file.pdf'): 
    print line 

Bên cạnh đó, Một số có thể google "trăn có được nội dung văn bản pdf" vì vậy đây là cách làm: (đây là làm thế nào tôi có ở đây)

from pyPdf import PdfFileReader 

def get_pdf_content(pdf_file_path): 
    with open(pdf_file_path) as f: 
     pdf_reader = PdfFileReader(f) 
     content = "\n".join(page.extractText().strip() for page in pdf_reader.pages) 
     content = ' '.join(content.split()) 
     return content 


print get_pdf_content('/path/to/file.pdf') 
Các vấn đề liên quan