zoukankan      html  css  js  c++  java
  • Excel、PDF文档解析

    from pdfminer.pdfparser import PDFParser
    from pdfminer.pdfdocument import PDFDocument
    from pdfminer.pdfpage import PDFTextExtractionNotAllowed, PDFPage
    from pdfminer.pdfinterp import PDFResourceManager,PDFPageInterpreter
    from pdfminer.layout import LAParams, LTTextBoxHorizontal
    from pdfminer.converter import PDFPageAggregator
    
    import sys
    import xlrd
    reload(sys)
    sys.setdefaultencoding('utf-8')
    
    
    def pdf_transform_text():
        print "开始解析pdf" + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        content = ''
        fp = open('tempPdfFile_new3.pdf', 'rb')
        # 来创建一个pdf文档分析器
        parser = PDFParser(fp)
        # 创建一个PDF文档对象存储文档结构
        document = PDFDocument(parser)
        # 检查文件是否允许文本提取
        if not document.is_extractable:
            raise PDFTextExtractionNotAllowed
        else:
            # 创建一个PDF资源管理器对象来存储共赏资源
            rsrcmgr = PDFResourceManager()
            # 设定参数进行分析
            laparams = LAParams()
            # 创建一个PDF设备对象
            # device=PDFDevice(rsrcmgr)
            device = PDFPageAggregator(rsrcmgr, laparams=laparams)
            # 创建一个PDF解释器对象
            interpreter = PDFPageInterpreter(rsrcmgr, device)
            # 处理每一页
            for page in PDFPage.create_pages(document):
                interpreter.process_page(page)
                # 接受该页面的LTPage对象
                layout = device.get_result()
                for x in layout:
                    if (isinstance(x, LTTextBoxHorizontal)):
                        content += x.get_text().encode('utf-8') + '
    '
        print "解析pdf成功" + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        return content.decode("utf-8")
    
    def parse_excel(url, filename):
        print '开始解析excel文档', filename, url
        req = WebRequests()
        inputStream = req.get(url, timeout=180)
        # 写入到本地暂存,方便解析成文本
        with open("tempExcelFile_new3.xls", "wb") as xls:
            xls.write(inputStream.content)
        data = xlrd.open_workbook("tempExcelFile_new3.xls")
        for i in range(len(data.sheets())):
            table = data.sheets()[i]
            print table.name
            print table.nrows
            for i in range(2, table.nrows):
                if len(table.row_values(i))>=12:
                    result = {}
                    result['company_name'] = table.row_values(i)[1]
                    result['province'] = table.row_values(i)[2]
                    result['industry'] = table.row_values(i)[3]
                    result['broker_company'] = table.row_values(i)[4]
                    result['broker_person'] = table.row_values(i)[5]
                    result['law_firm'] = table.row_values(i)[6]
                    result['laywyer'] = table.row_values(i)[7]
                    result['accounting_firm'] = table.row_values(i)[8]
                    result['accountant'] = table.row_values(i)[9]
                    # result[''] = table.row_values(i)[10] #挂牌同时发行((是/否))
                    result['progress'] = table.row_values(i)[11]
                    if len(table.row_values(i))==13:
                        result['receive_date'] = table.row_values(i)[12]
                    saveOrUpdateNew3CompanyBaseInfo(result)

    注意:Excelt在解析时要看一下有没有多个sheet。该pdf解析只能解析文本内容的pdf

  • 相关阅读:
    P2761 软件补丁问题
    CF1335F Robots on a Grid
    [bzoj2088]P3505 [POI2010]TEL-Teleportation
    CF1335E Three Blocks Palindrome
    P3831 [SHOI2012]回家的路
    P4568 [JLOI2011]飞行路线(分层图)
    P4774 [NOI2018]屠龙勇士
    P2480 [SDOI2010]古代猪文
    CF #632 (Div. 2) 对应题号CF1333
    BSGS 和扩展
  • 原文地址:https://www.cnblogs.com/liangping/p/7669988.html
Copyright © 2011-2022 走看看