学习轨道报销明细这个xlsx,然后学习滴滴出行行程单这个pdf,我每个月都要手动把滴滴出行行程单填入...

Tạo vào: 14 tháng 3, 2025

Trả lời bằng O1 bởi Chat01

Câu hỏi

轨道交通费报销明细2025年2月王维乾.xlsx
滴滴出行行程报销单(11).pdf

学习轨道报销明细这个xlsx,然后学习滴滴出行行程单这个pdf,我每个月都要手动把滴滴出行行程单填入报销明细这个xlsx,给我写个网页自动填入,或者让ai帮我识别后填入之类的,好好思考解决这个问题

Suy nghĩ

Planning the extraction

The task is to automate importing data from a Didi travel itinerary PDF into an Excel file using OCR and text extraction libraries, then parsing the data into Excel fields, possibly with a local web server.

Charting a course

I’m considering a Python script to parse the PDF using PyPDF2 or pdfplumber, open the Excel file with openpyxl, and fill the columns based on data like date or mileage.

Mapping the steps

The plan involves uploading the PDF, extracting data with a script, and using openpyxl to fill the XLSX, considering Flask, Node.js, or AI-based methods.

Reasoned for 6 seconds

Trả lời

下面给你一个完整的思路,帮助你从「滴滴出行行程单 PDF」中自动读取行程、金额等信息,并写入到「轨道交通费报销明细.xlsx」中,最终实现一键/自动化处理。整体流程可以分为以下几步:


1. 解析 PDF 行程单:提取关键信息

  1. 文本或表格提取

    • 如果 PDF 里是可复制的文字(非纯图片),那么可以使用 Python 的 pdfplumber / PyPDF2 / camelot-py 等库,从 PDF 中按表格或按行提取内容。
    • 如果 PDF 是图片形式的扫描件,需要先用 OCR(例如 pytesseract)识别文字,然后再进行后续的表格解析逻辑。
  2. 识别每一行行程

    • 滴滴行程单一般会包含:
      • 序号
      • 车型
      • 上车时间
      • 城市
      • 起点/终点
      • 里程
      • 金额
    • 通过正则或字符串操作,将上述字段分离出来并存储在一个数据结构中(例如:Python 的 list[dict],每个 dict 存一条行程信息)。
  3. 数据清洗

    • 去掉无用的空格、换行等。
    • 确保提取到的日期、金额等转成标准的数据类型(日期可以转 datetime.date 或字符串,金额可以转 float)。

2. 写入 Excel「报销明细」:匹配并填写相应字段

  1. 定位 Excel 模板中对应字段

    • 先查看你的「轨道交通费报销明细.xlsx」,里面一般会有如下列:
      • 乘车日期 / 乘车时间
      • 出发地 / 目的地
      • 费用(金额)
      • 里程
      • 车型 / 备注
    • 看看你的表格是如何安排的:是每一行记录一笔?还是还有别的合并单元格?是否有固定表头?
    • 在代码里,你可以用 openpyxl 打开该 Excel,再定位到具体工作表和单元格。
  2. 遍历「提取到的行程信息」并插入到 Excel 对应行

    • 比如用 openpyxl 对应操作:
      python
      from 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')
    • 填写完后,你会得到一个新的 Excel 文件(或直接覆盖原文件)。

3. 打造一个可视化/网页交互界面

如果你想做成一个“网页上传 PDF 一键输出填好的 Excel”的小型应用,可以做以下工作:

  1. 后端:Python + Flask

    • 在 Flask 项目里写一个 /upload 接口。
    • 用户在网页里上传 PDF,服务器端收到文件后,先把 PDF 暂存到本地,然后运行上面提到的 PDF 解析逻辑和 Excel 写入逻辑。
    • 最后返回给用户一个可下载的“已填写完成的 Excel”文件。
  2. 前端:一个简易的 HTML 上传表单

    • 可以用最简单的 <form> + <input type="file"> + 提交按钮。
    • 提交到 Flask /upload 路由。
    • 服务器处理后,将生成的 Excel 文件以附件的方式返回给浏览器进行下载。

示例伪代码(Flask):

python
import 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。


4. 引入 AI 自动识别改进

  1. 表格结构复杂

    • 如果滴滴行程单的布局随时间可能变化,有时是纯文本,有时是复杂表格,pdfplumber 等常规解析比较难处理,可以尝试加上 AI OCR(如阿里云/百度云/天翼云的文字识别 API),返回结构化的表格信息。
    • 再或者用大模型辅助解析,给大模型一段指令“这是 PDF 每一行里包含的数据,你把它解析成 JSON 结构”,然后对每个 PDF 页面做 Prompt 解析,获取结构化结果。
  2. 异常行程识别

    • 如果有些行程需要自动添加备注,比如公司规定夜间打车要加特殊备注,可以结合 AI 或者规则,自动判断并填写。
  3. 数据校验

    • 可以让 AI 或者规则来校验里程、金额、车型等是否合理(例如:同一天多次重复地点往返,金额过高等),从而减少人工对账。

5. 进一步优化与落地建议

  • 自动命名
    • 每个月批量处理多个 PDF 时,可以根据月份或出行人自动给输出Excel命名,比如 2025年2月-王维乾-行程报销.xlsx
  • 自动邮件发送
    • 在服务器上做好后端,填写好报销明细的 Excel 文件后,自动通过公司内网邮箱发送到财务,减轻手动传文件的麻烦。
  • 权限与安全
    • 如果需要多人使用,注意服务器访问权限和上传文件的安全性。
  • Docker 部署
    • 可以把整套 Python + Flask + openpyxl + pdfplumber + OCR 依赖都封装进 Docker,方便在公司内部服务器落地、便于维护。

总结

借助 Python (pdfplumber / openpyxl 等) 或者前端 + 后端 Web 服务,你可以快速实现一个“小工具/小网站”,自动将「滴滴出行行程单 PDF」的信息导入到「轨道交通费报销明细.xlsx」。如果格式有变动,则需要定期微调 PDF 解析部分。要是想更灵活、更少写规则,可以把大模型/AI OCR 也整合进来,自动识别并输出结构化数据,再填到 Excel 里。

这样就可以彻底摆脱每月重复的手动复制行程数据、手动填写报销明细的麻烦了。

Câu hỏi

你帮我写出来一个网页

Suy nghĩ

Crafting a webpage

I'm working on a minimal Flask code for a webpage with HTML for PDF and Excel uploads, processing, and an attachment return. It will be streamlined into a single Python file.

Reasoned for a couple of seconds

Trả lời

下面给你示例代码,演示如何用 Flask + pdfplumber + openpyxl 构建一个简易网页,实现以下流程:

  1. 在浏览器上传「滴滴出行行程单 PDF」和「报销明细 Excel」。
  2. 解析 PDF 内容,并将行程信息写入 Excel。
  3. 最后自动生成并下载一份新填好的 Excel 给用户。

注意:这只是一个示例,运行时需要在服务器上安装 pdfplumber, openpyxl, Flask 等库。并且需要根据你的 PDF 和 Excel 实际格式做适当调整,尤其是 parse_pdffill_excel_with_info 函数。


1. 建立项目目录

假设你在某个文件夹下(如 ~/didi_project)创建以下文件结构:

text
didi_project ├── app.py # 主 Flask 程序 ├── requirements.txt # Python 依赖清单 ├── templates │ └── index.html # 前端HTML模板 └── static └── ... # 如果需要放置一些静态文件,比如CSS/JS

requirements.txt(示例依赖)

text
Flask==2.2.3 pdfplumber==0.7.6 openpyxl==3.1.2 PyPDF2==3.0.1 # 可选,如果你需要更底层的PDF操作 pytesseract==0.3.10 # 如果是图片PDF要OCR才需要

安装依赖:

bash
pip install -r requirements.txt

2. 前端: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>

3. 后端:app.py (Flask 主程序)

下面是一份示例完整代码。你只需要在命令行进入本目录,运行 python app.py 即可。然后在浏览器访问 http://127.0.0.1:5000 即可看到上传页面。

python
import 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)

运行项目

  1. 启动 Flask

    bash
    cd didi_project python app.py
  2. 在浏览器访问:
    http://127.0.0.1:5000/

  3. 上传 PDF & Excel 后,等待服务器解析完,即可自动下载生成的 Excel 文件。


4. 根据实际情况做自定义

  • 列对应关系
    • 上面的示例里,parse_pdf 里定义了 'date','start','end','car_type','mile','price','city' 等字段;
    • fill_excel_with_info 里设置了 column=1 填日期、column=2 填起点、column=3 填终点、等。
    • 如果你的 Excel 表头或者列顺序不一样,请根据需要去调整列对应关系。
  • OCR 场景
    • 如果你的 PDF 是纯图片,需要结合 pytesseract 或者云端 OCR API,将页面先转成文字/表格再进行后续解析。
  • 日期格式
    • 如果你想写入 Excel 时就做成日期单元格,可在 Python 端用 datetime 类型赋值,这样 Excel 里就是日期格式。
  • 字段校验
    • 如果有行程里金额、里程为空或格式不对,需要在代码中做好异常处理,或者提示用户 PDF 格式有误。

通过这种方式,就可以做出一个最简单的“网页 + 一键生成报销单”小工具。后续还可以扩展,做更完善的字段校验、批量处理、数据库记录等功能。

Chia sẻ Q&A này