tags:

views:

224

answers:

1

hi, i working with pdf files, and i have some code to read from a pdf file,, but is there a way to read line by line from the pdf file( not pages ) ,, using Pypdf,, Python 2.6 ,, on windows ???

here is the code for reading the pdf pages::

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

Update:

the call code is this::

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

Looks like what you have is a large chunk of text data that you want to interpret line-by-line.

You can use the StringIO class to wrap that content as a seekable file-like object:

>>> 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'

In your case, do:

from StringIO import StringIO

pdfContent = StringIO(getPDFContent("test.pdf").encode("ascii", "ignore"))
    for line in pdfContent:
        doSomething(line.strip())
MikeyB
yea, but where i could but this in my code,, because i cant make it work ???
Rami Jarrar
same problem, this is not work it give me the whole page,, i just want line by line:)
Rami Jarrar