吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2066|回复: 20
收起左侧

[Python 原创] 发票pdf文件解析

[复制链接]
xuhuanchao 发表于 2024-1-18 13:25
借助pdfplumber  解析
效果如下:

{'发票号码(FPHM)': '24322000000011529984', '开票日期(KPRQ)': '2024年01月11日', '合计(HJ)': '1205.94', '购方': '91320213586657279T', '销方': '91320214MAD1N7EN36', '价税合计(JSHJ)': '1218.00', '项目(XM)-1': '餐饮 11205.940594059405 1205.94 1% 12.06'}

1、安装
[Python] 纯文本查看 复制代码
pip install pdfplumber  -i https://pypi.tuna.tsinghua.edu.cn/simple


2、全部代码
[Python] 纯文本查看 复制代码
def getPdfText2(path):
    with pdfplumber.open(path) as pdf:
        page = pdf.pages[0]
        text = page.extract_text()
        print(text)
        # 提取发票表格上方内容
        invoice = {}
        ftype: int = 0  # 取购销方纳税识别号方式  1 是 纳税人识别号: 91320213586657279T ,2是只有18位数字
        item = re.search(r'发票号码(:|: |:)(\d+)', text)
        if item is not None:
            item = item.group()
            item = re.sub(r'发票号码(:|: |:)', '', item)
            item = item.replace(' ', '')
            invoice.update({"发票号码(FPHM)": item})

        item = re.search(r'开票日期(:|: |:)(.*)', text)
        if item is not None:
            item = item.group()
            item = re.sub(r'开票日期(:|: |:)', '', item)
            item = item.replace(' ', '')
            invoice.update({"开票日期(KPRQ)": item})

        item = re.search(r'机器编号(:|: |:)(\d+)', text)
        if item is not None:
            item = item.group()
            item = re.sub(r'机器编号(:|: |:)', '', item)
            item = item.replace(' ', '')
            invoice.update({"机器编号(JQBH)": item})

        item = re.search(r'发票代码(:|: |:)(\d+)', text)
        if item is not None:
            item = item.group()
            item = re.sub(r'发票代码(:|: |:)', '', item)
            item = item.replace(' ', '')
            invoice.update({"发票代码(FPDM)": item})

        item = re.search(r'校验码(:|: |:)(\d+)', text)
        if item is not None:
            item = item.group()
            item = re.sub(r'校验码(:|: |:)', '', item)
            item = item.replace(' ', '')
            invoice.update({"校验码(JYM)": item})

        item = re.search(r'合(\s+)计(.*)', text)
        if item is not None:
            item = item.group()
            item = item.replace(' ', '').replace('合计', '')
            item = re.search(r'¥(\d+).(\d+)', item).group()
            item = item.replace('¥', '')
            invoice.update({"合计(HJ)": item})

        # 购销方 纳税人识别号  方式1 (纳税人识别号: 913202006829704176)
        items = re.findall(r'纳税人识别号(:|: |:)(\w+)', text)
        if len(items) >= 2:
            invoice.update({"购方": items[0][1]})
            invoice.update({"销方": items[1][1]})
            ftype = 1

        if ftype == 0:
            # 购销方 纳税人识别号  方式2 (只有18位数字)
            items = re.findall(r'[0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}', text)
            if len(items) == 2:
                invoice.update({"购方": items[0]})
                invoice.update({"销方": items[1]})
            if len(items) >= 2:
                invoice.update({"购方": items[1]})
                invoice.update({"销方": items[2]})

        item = re.search(r'(小写)(.*)', text)
        if item is not None:
            item = item.group()
            item = item.replace(' ', '').replace('小写)¥', '').replace('小写)¥', '')
            invoice.update({"价税合计(JSHJ)": item})

        items = re.findall(r'\*[\u4e00-\u9fa5]+\*(.*)', text)
        i: int = 1
        for item in items:
            invoice.update({"项目(XM)-" + str(i): item})
            i = i + 1

        print(invoice)

免费评分

参与人数 2吾爱币 +8 热心值 +2 收起 理由
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
soughing + 1 + 1 我很赞同!

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

赵世哲 发表于 2024-1-18 13:43
谢谢楼主分享
头像被屏蔽
tl;dr 发表于 2024-1-18 13:54
wacka 发表于 2024-1-18 19:52
soughing 发表于 2024-1-19 07:31
谢谢楼主分享
wosn 发表于 2024-1-19 10:10
感谢分享
qwuiop789 发表于 2024-1-19 18:29
解析结果写出来
Zhui 发表于 2024-1-19 19:25
谢谢分享
dongwang_fl 发表于 2024-1-21 08:34

感谢分享
starlisir 发表于 2024-1-22 09:39
谢谢楼主分享....
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-8 23:24

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表