发票pdf文件解析

借助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)
相关推荐
engchina4 分钟前
Python PDF处理库深度对比:PyMuPDF、pypdfium2、pdfplumber、pdfminer的关系与区别
开发语言·python·pdf
浮桥6 分钟前
vue3实现pdf文件预览 - vue-pdf-embed
前端·vue.js·pdf
Lo-Y-eH11 分钟前
Openpyxl:Python操作Excel的利器
python·excel
DAWN_T1716 分钟前
Transforms
pytorch·python·机器学习·jupyter·pycharm
一百天成为python专家29 分钟前
python库之jieba 库
开发语言·人工智能·python·深度学习·机器学习·pycharm·python3.11
27669582921 小时前
tiktok 弹幕 逆向分析
java·python·tiktok·tiktok弹幕·tiktok弹幕逆向分析·a-bogus·x-gnarly
cylat1 小时前
Day59 经典时序预测模型3
人工智能·python·深度学习·神经网络
嘉恩督1 小时前
视频人脸处理——人脸面部动作提取
python·音视频
WJ.Polar1 小时前
Python数据容器-集合set
开发语言·python
smppbzyc2 小时前
2025年亚太杯(中文赛项)数学建模B题【疾病的预测与大数据分析】原创论文讲解(含完整python代码)
python·数学建模·数据分析·数学建模竞赛·亚太杯数学建模·亚太杯