PDFMiner读取pdf文件

PDFMiner读取pdf文件

文档地址:
http://www.unixuser.org/~euske/python/pdfminer/index.html

下载得到文件:
pdfminer-20100322.tar.gz

uppack it. Run setup.py to install:
E:\tools\python\pdfminer\pdfminer-20100322>python setup.py install

try the pdf2txt.py comand:
E:\tools\python\pdfminer\pdfminer-20100322>pdf2txt.py samples/simple1.pdf
Hello

World

Hello

World

H e l l o

W o r l d

H e l l o

W o r l d

解析得到了这个PDF的内容,证明我们安装成功了。

测试解析我需要解析的pdf内容:
解析保存为txt
pdf2txt.py -o "01-27 Mar 10.txt" "01-27 Mar 10.pdf"
解析保存为xml
pdf2txt.py -t xml -o "01-27 Mar 10.xml" "01-27 Mar 10.pdf"
解析保存为html
pdf2txt.py -t html -o "01-27 Mar 10.html" "01-27 Mar 10.pdf"

其中比较适合我使用的是txt,我将pdf中的文字内容解析为txt后,读取里面的信息。

参考c:\Python26\Scripts\pdf2txt.py的代码,写了个测试pdfTest.py来从pdf得到txt:
'''
Created on 2010-3-27

@author: sillycat
'''
import sys
from pdfminer.pdfparser import PDFDocument, PDFParser
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter, process_pdf
from pdfminer.pdfdevice import PDFDevice
from pdfminer.converter import XMLConverter, HTMLConverter, TextConverter, TagExtractor
from pdfminer.cmapdb import CMapDB
from pdfminer.layout import LAParams

outtype = "txt"
outfile = "d:/data/pdf1.txt"
codec = 'utf-8'
args = ['d:/data/pdf1.pdf']

rsrc = PDFResourceManager()
outfp = file(outfile, 'w')
device = TextConverter(rsrc, outfp, codec=codec, laparams=None)
for fname in args:
        fp = file(fname, 'rb')
        process_pdf(rsrc, device, fp, None, maxpages=0, password='')
        fp.close()
device.close()
outfp.close()

你可能感兴趣的:(html,c,xml,python,FP)