복붙노트

[PYTHON] PyPdf를 사용하여 pdf 파일의 한 줄씩 읽는 방법?

PYTHON

PyPdf를 사용하여 pdf 파일의 한 줄씩 읽는 방법?

pdf 파일에서 읽을 코드가 있습니다. Windows에서 Pypdf, Python 2.6을 사용하여 pdf 파일 (페이지가 아님)에서 한 줄씩 읽는 방법이 있습니까?

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

최신 정보:

전화 코드는 다음과 같습니다.

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

해결법

  1. ==============================

    1.당신이 가지고있는 것처럼 보이는 것은 줄 단위로 해석하기를 원하는 많은 양의 텍스트 데이터입니다.

    당신이 가지고있는 것처럼 보이는 것은 줄 단위로 해석하기를 원하는 많은 양의 텍스트 데이터입니다.

    StringIO 클래스를 사용하여 해당 내용을 검색 가능한 파일 형 객체로 래핑 할 수 있습니다.

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

    귀하의 경우 :

    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())
    
  2. ==============================

    2.

    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 
    
  3. ==============================

    3.yield와 PdfFileReader.pages를 사용하면 사물을 단순화 할 수 있으며,

    yield와 PdfFileReader.pages를 사용하면 사물을 단순화 할 수 있으며,

    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
    

    또한, 일부는 "python get pdf content text"라는 구글이 될 수 있습니다. 그래서 여기에 있습니다 : (내가 여기에있는 방법입니다)

    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')
    
  4. from https://stackoverflow.com/questions/2481945/how-to-read-line-by-line-in-pdf-file-using-pypdf by cc-by-sa and MIT license