学习轨道报销明细这个xlsx,然后学习滴滴出行行程单这个pdf,我每个月都要手动把滴滴出行行程单填入...
Tạo vào: 14 tháng 3, 2025
Tạo vào: 14 tháng 3, 2025
下面给你一个完整的思路,帮助你从「滴滴出行行程单 PDF」中自动读取行程、金额等信息,并写入到「轨道交通费报销明细.xlsx」中,最终实现一键/自动化处理。整体流程可以分为以下几步:
文本或表格提取
pdfplumber
/ PyPDF2
/ camelot-py
等库,从 PDF 中按表格或按行提取内容。pytesseract
)识别文字,然后再进行后续的表格解析逻辑。识别每一行行程
list[dict]
,每个 dict
存一条行程信息)。数据清洗
datetime.date
或字符串,金额可以转 float
)。定位 Excel 模板中对应字段
openpyxl
打开该 Excel,再定位到具体工作表和单元格。遍历「提取到的行程信息」并插入到 Excel 对应行
openpyxl
对应操作:
pythonfrom openpyxl import load_workbook wb = load_workbook('轨道交通费报销明细.xlsx') ws = wb.active # 或者 wb['Sheet1'] 按需选择 current_row = 2 # 假设第1行是表头,从第2行开始填写 for item in extracted_info: # extracted_info 就是从 PDF 得到的行程列表 ws.cell(row=current_row, column=1).value = item['上车时间'] # 乘车日期 ws.cell(row=current_row, column=2).value = item['起点'] ws.cell(row=current_row, column=3).value = item['终点'] ws.cell(row=current_row, column=4).value = item['车型'] ws.cell(row=current_row, column=5).value = item['里程'] ws.cell(row=current_row, column=6).value = item['金额'] ws.cell(row=current_row, column=7).value = item['城市'] # ...看你的明细表还有哪些列就一并写入 current_row += 1 wb.save('轨道交通费报销明细-自动填写后.xlsx')
如果你想做成一个“网页上传 PDF 一键输出填好的 Excel”的小型应用,可以做以下工作:
后端:Python + Flask
/upload
接口。前端:一个简易的 HTML 上传表单
<form>
+ <input type="file">
+ 提交按钮。/upload
路由。示例伪代码(Flask):
pythonimport os import uuid import pdfplumber from flask import Flask, request, send_file from openpyxl import load_workbook app = Flask(__name__) @app.route('/', methods=['GET']) def index(): return ''' <html> <body> <h1>滴滴出行行程单 -> 报销明细</h1> <form action="/upload" method="post" enctype="multipart/form-data"> <p>上传滴滴行程单PDF:<input type="file" name="pdf_file"></p> <p>上传报销明细Excel:<input type="file" name="excel_file"></p> <p><input type="submit" value="开始处理"></p> </form> </body> </html> ''' @app.route('/upload', methods=['POST']) def upload(): pdf_file = request.files['pdf_file'] excel_file = request.files['excel_file'] # 临时保存 pdf_path = os.path.join('/tmp', f"{uuid.uuid4()}.pdf") excel_path = os.path.join('/tmp', f"{uuid.uuid4()}.xlsx") pdf_file.save(pdf_path) excel_file.save(excel_path) # 读取PDF提取行程信息 (示意) extracted_info = parse_pdf(pdf_path) # 你写的自定义函数 # 写入Excel new_excel_path = fill_excel_with_info(excel_path, extracted_info) # 你写的自定义函数 # 返回处理完的Excel给用户下载 return send_file(new_excel_path, as_attachment=True, download_name='报销明细-自动填写.xlsx') def parse_pdf(pdf_path): # 用 pdfplumber 或其他方法抽取表格 extracted_info = [] with pdfplumber.open(pdf_path) as pdf: for page in pdf.pages: table = page.extract_table() if not table: continue # 这里根据滴滴行程单的表头/格式 解析出有效行 # 例如:table[0] 是表头,后面是数据行 # 做一些清洗、类型转换等 # ... return extracted_info def fill_excel_with_info(excel_path, extracted_info): from openpyxl import load_workbook wb = load_workbook(excel_path) ws = wb.active # 或按名称选择 current_row = 2 # 假设第1行为表头 for item in extracted_info: ws.cell(row=current_row, column=1).value = item['上车时间'] ws.cell(row=current_row, column=2).value = item['起点'] ws.cell(row=current_row, column=3).value = item['终点'] ws.cell(row=current_row, column=4).value = item['金额'] # 按自己需要填写的列来 current_row += 1 new_path = excel_path.replace('.xlsx', '-自动填写.xlsx') wb.save(new_path) return new_path if __name__ == '__main__': app.run(debug=True)
这样,你就可以在浏览器里访问 http://127.0.0.1:5000
,上传滴滴 PDF 和一个 Excel 模板,点击提交后,就能生成并下载填好的新 Excel。
表格结构复杂
异常行程识别
数据校验
2025年2月-王维乾-行程报销.xlsx
。借助 Python (pdfplumber / openpyxl 等) 或者前端 + 后端 Web 服务,你可以快速实现一个“小工具/小网站”,自动将「滴滴出行行程单 PDF」的信息导入到「轨道交通费报销明细.xlsx」。如果格式有变动,则需要定期微调 PDF 解析部分。要是想更灵活、更少写规则,可以把大模型/AI OCR 也整合进来,自动识别并输出结构化数据,再填到 Excel 里。
这样就可以彻底摆脱每月重复的手动复制行程数据、手动填写报销明细的麻烦了。
你帮我写出来一个网页
下面给你示例代码,演示如何用 Flask + pdfplumber + openpyxl 构建一个简易网页,实现以下流程:
注意:这只是一个示例,运行时需要在服务器上安装
pdfplumber
,openpyxl
,Flask
等库。并且需要根据你的 PDF 和 Excel 实际格式做适当调整,尤其是parse_pdf
和fill_excel_with_info
函数。
假设你在某个文件夹下(如 ~/didi_project
)创建以下文件结构:
textdidi_project ├── app.py # 主 Flask 程序 ├── requirements.txt # Python 依赖清单 ├── templates │ └── index.html # 前端HTML模板 └── static └── ... # 如果需要放置一些静态文件,比如CSS/JS
textFlask==2.2.3 pdfplumber==0.7.6 openpyxl==3.1.2 PyPDF2==3.0.1 # 可选,如果你需要更底层的PDF操作 pytesseract==0.3.10 # 如果是图片PDF要OCR才需要
安装依赖:
bashpip install -r requirements.txt
templates/index.html
这是一个简单的HTML页面,用于上传 PDF 和 Excel 文件:
html<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8" /> <title>滴滴行程单自动填写</title> </head> <body> <h1>滴滴出行行程单 -> 报销明细(示例)</h1> <form action="/upload" method="post" enctype="multipart/form-data"> <p>上传滴滴行程单 PDF: <input type="file" name="pdf_file" accept=".pdf" required> </p> <p>上传报销明细 Excel: <input type="file" name="excel_file" accept=".xlsx" required> </p> <button type="submit">开始处理</button> </form> </body> </html>
app.py
(Flask 主程序)下面是一份示例完整代码。你只需要在命令行进入本目录,运行 python app.py
即可。然后在浏览器访问 http://127.0.0.1:5000 即可看到上传页面。
pythonimport os import uuid import pdfplumber from flask import Flask, request, send_file, render_template from openpyxl import load_workbook app = Flask(__name__) @app.route('/', methods=['GET']) def index(): """ 显示上传页面 """ return render_template('index.html') @app.route('/upload', methods=['POST']) def upload(): """ 处理上传的 PDF 和 Excel: 1. 解析 PDF 提取行程信息 2. 写入 Excel 3. 返回处理后的 Excel 文件给用户下载 """ # 获取上传的文件 pdf_file = request.files['pdf_file'] excel_file = request.files['excel_file'] # 临时保存 pdf_path = os.path.join('tmp', f"{uuid.uuid4()}.pdf") excel_path = os.path.join('tmp', f"{uuid.uuid4()}.xlsx") # 创建临时文件夹 os.makedirs('tmp', exist_ok=True) pdf_file.save(pdf_path) excel_file.save(excel_path) # 1. 解析 PDF 得到行程信息 extracted_info = parse_pdf(pdf_path) # 2. 将行程信息填入 Excel new_excel_path = fill_excel_with_info(excel_path, extracted_info) # 3. 把生成的 Excel 通过 send_file 下载给用户 return send_file(new_excel_path, as_attachment=True, download_name='报销明细-自动填写.xlsx') def parse_pdf(pdf_path): """ 用 pdfplumber 提取行程信息。 根据滴滴行程单的实际格式,做相应解析/清洗逻辑。 返回的 extracted_info 是个列表,列表里每个元素是字典: [ { 'date': '2025-02-06', 'start': '回兴|上湾路地铁站3口', 'end': '渝北区|石盘河地铁站1A口', 'mile': 1.9, 'price': 11.18, 'city': '重庆市', 'car_type': '特惠快车' }, ... ] """ extracted_info = [] with pdfplumber.open(pdf_path) as pdf: for page in pdf.pages: # 获取表格(如果有多张表格,则需要视情况合并) table = page.extract_table() if not table: continue # table 通常是一个二维数组,每一行是一个列表,第一行可能是表头 # 比如滴滴行程单的表头为:序号 / 车型 / 上车时间 / 城市 / 起点 / 终点 / 里程 / 金额 / 备注 # 先看看第一行是不是表头 # 你可以先看第0行,判断是否包含字段名 if len(table) > 1: # 从 table 的第1行开始(跳过表头),逐行解析 for row in table[1:]: if len(row) < 8: continue # 数据不完整就跳过 # row例子:['1','特惠快车','02-06 23:30 周四','重庆市','回兴|...','渝北区|...','1.9','11.18','(备注可能省略)'] # 下面要根据真实情况把信息解析出来 seq = row[0] # 序号 car_type = row[1] time_info = row[2] # 包含 "02-06 23:30 周四" city = row[3] start = row[4] end = row[5] mileage = row[6] price = row[7] # 时间里通常包含 月-日 时:分+星期几,你也可以用正则把日期分离 # 简单处理:取 time_info 的前5位"02-06" 作为日期 date_part = time_info[:5] # '02-06' # 这里你也可以拼接一个年,如 '2025-' + date_part => '2025-02-06' full_date = f"2025-{date_part}" # 转数字 try: mile_float = float(mileage) except: mile_float = None try: price_float = float(price) except: price_float = None item_dict = { 'date': full_date, 'car_type': car_type, 'city': city, 'start': start, 'end': end, 'mile': mile_float, 'price': price_float } extracted_info.append(item_dict) return extracted_info def fill_excel_with_info(excel_path, extracted_info): """ 用 openpyxl 打开 Excel, 将 extracted_info 里每条行程写到 Excel 中。 返回新生成的 Excel 路径。 """ wb = load_workbook(excel_path) ws = wb.active # 这里也可以指定 ws = wb['Sheet1'] 等 # 假设你的 Excel 第1行是表头,从第2行开始写 current_row = 2 for item in extracted_info: ws.cell(row=current_row, column=1).value = item.get('date') ws.cell(row=current_row, column=2).value = item.get('start') ws.cell(row=current_row, column=3).value = item.get('end') ws.cell(row=current_row, column=4).value = item.get('car_type') ws.cell(row=current_row, column=5).value = item.get('mile') ws.cell(row=current_row, column=6).value = item.get('price') ws.cell(row=current_row, column=7).value = item.get('city') # 如果还有其他列,如备注、费用类型等,可以继续写 current_row += 1 # 生成一个新文件名,以免覆盖原文件 new_path = excel_path.replace('.xlsx', '-自动填写.xlsx') wb.save(new_path) return new_path if __name__ == '__main__': # 建议 debug=True 方便查看日志 app.run(host='0.0.0.0', port=5000, debug=True)
启动 Flask
bashcd didi_project python app.py
在浏览器访问:
http://127.0.0.1:5000/
上传 PDF & Excel 后,等待服务器解析完,即可自动下载生成的 Excel 文件。
parse_pdf
里定义了 'date','start','end','car_type','mile','price','city'
等字段;fill_excel_with_info
里设置了 column=1
填日期、column=2
填起点、column=3
填终点、等。pytesseract
或者云端 OCR API,将页面先转成文字/表格再进行后续解析。datetime
类型赋值,这样 Excel 里就是日期格式。通过这种方式,就可以做出一个最简单的“网页 + 一键生成报销单”小工具。后续还可以扩展,做更完善的字段校验、批量处理、数据库记录等功能。