在python中将pdf转换为text/html,以便我可以解析它

Tho*_*sen 4 python pdf parsing text

我有以下示例代码,我从欧洲议会网站上下载了一份特定立法提案的pdf:

编辑:我最终只是获取链接并将其提供给adobes在线转换工具(请参阅下面的代码):

import mechanize
import urllib2
import re
from BeautifulSoup import *

adobe = "http://www.adobe.com/products/acrobat/access_onlinetools.html"

url = "http://www.europarl.europa.eu/oeil/search_reference_procedure.jsp"

def get_pdf(soup2):
    link = soup2.findAll("a", "com_acronym")
    new_link = []
    amendments = []
    for i in link:
        if "REPORT" in i["href"]:
            new_link.append(i["href"])
    if new_link == None:
        print "No A number"
    else:
        for i in new_link:
            page = br.open(str(i)).read()
            bs = BeautifulSoup(page)
            text = bs.findAll("a")
            for i in text:
                if re.search("PDF", str(i)) != None:
                    pdf_link = "http://www.europarl.europa.eu/" + i["href"]
            pdf = urllib2.urlopen(pdf_link)
            name_pdf = "%s_%s.pdf" % (y,p)
            localfile = open(name_pdf, "w")
            localfile.write(pdf.read())
            localfile.close()

            br.open(adobe)
            br.select_form(name = "convertFrm")
            br.form["srcPdfUrl"] = str(pdf_link)
            br["convertTo"] = ["html"]
            br["visuallyImpaired"] = ["notcompatible"]
            br.form["platform"] =["Macintosh"]
            pdf_html = br.submit()

            soup = BeautifulSoup(pdf_html)


page = range(1,2) #can be set to 400 to get every document for a given year
year = range(1999,2000) #can be set to 2011 to get documents from all years

for y in year:
    for p in page:
        br = mechanize.Browser()
        br.open(url)
        br.select_form(name = "byReferenceForm")
        br.form["year"] = str(y)
        br.form["sequence"] = str(p)
        response = br.submit()
        soup1 = BeautifulSoup(response)
        test = soup1.find(text="No search result")
        if test != None:
            print "%s %s No page skipping..." % (y,p)
        else:
            print "%s %s  Writing dossier..." % (y,p)
            for i in br.links(url_regex="file.jsp"):
                link = i
            response2 = br.follow_link(link).read()
            soup2 = BeautifulSoup(response2)
            get_pdf(soup2)
Run Code Online (Sandbox Code Playgroud)

在get_pdf()函数中,我想将pdf文件转换为python中的文本,因此我可以解析文本以获取有关立法过程的信息.谁能解释我怎么做到这一点?

托马斯

Jac*_*man 11

听起来你找到了一个解决方案,但是如果你想在没有Web服务的情况下这样做,或者你需要根据PDF页面上的精确位置来抓取数据,我可以建议我的库,pdfquery吗?它基本上将PDF转换为lxml树,可以将其作为XML吐出,或者使用XPath,PyQuery或其他任何您想要使用的内容进行解析.

要使用它,一旦将文件保存到磁盘,您将返回pdf = pdfquery.PDFQuery(name_pdf),或者如果您不需要保存它,则直接传入urllib文件对象.要使用BeautifulSoup解析XML,您可以这样做pdf.tree.tostring().

如果你不介意使用JQuery风格的选择器,那么有一个带有位置扩展的PyQuery接口,这可以非常方便.例如:

balance = pdf.pq(':contains("Your balance is")').text()
strings_near_the_bottom_of_page_23 = [el.text for el in pdf.pq('LTPage[page_label=23] :in_bbox(0, 0, 600, 200)')]
Run Code Online (Sandbox Code Playgroud)


loe*_*org 2

这不完全是魔法。我建议

  • 将 PDF 文件下载到临时目录,
  • 调用外部程序将文本提取到(临时)文本文件中,
  • 读取文本文件。

对于文本提取命令行实用程序,您有多种可能性,并且可能还有链接中未提及的其他实用程序(可能基于 Java)。首先尝试一下,看看它们是否符合您的需求。也就是说,分别尝试每个步骤(查找链接、下载文件、提取文本),然后将它们拼凑在一起。如需呼叫,请使用subprocess.Popensubprocess.call()