You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

需求:匹配Excel与PDF发票号并写入Excel B列

发票号匹配写入Excel实现

需求说明:
我有一个Excel文件,其A列包含发票号,另有一个PDF文件。若PDF中的发票号与Excel中的发票号匹配,需将该号码写入Excel的B列。

以下是实现代码:

import openpyxl
import pdfplumber

# 加载Excel文件 
excel_file_path = 'path/to/your/excel/file.xlsx'
workbook = openpyxl.load_workbook(excel_file_path)
sheet = workbook.active

# 读取Excel中的发票号
invoice_numbers_excel = [row[0].value for row in sheet.iter_rows(min_row=2, values_only=True)]

# 加载PDF文件
pdf_file_path = 'path/to/your/pdf/file.pdf'
with pdfplumber.open(pdf_file_path) as pdf:
    page_text = pdf.pages[0].extract_text()
    invoice_numbers_pdf = [num.strip() for num in page_text.split('\n') if num.strip()]

# 匹配并写入符合条件的号码
for row_idx, invoice_num_excel in enumerate(invoice_numbers_excel, start=2):
    if invoice_num_excel in invoice_numbers_pdf:
        sheet.cell(row=row_idx, column=2, value=invoice_num_excel)

# 保存修改后的Excel文件
workbook.save('datafile.xlsx')

内容的提问来源于stack exchange,提问作者Giorgi Gurabanidze

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 18:33:24