【求助】通过python脚本调用ocr识别表格中的pdf/jpg文件提取相关信息代码求助

希望通过python语言实现以下功能:
1、我在seatable平台新建了一个表格,名称为“发票管理”,子表名称为“Invoices”

2、“Invoices”子表中有一列名为“发票文件”的列,该列有格式可能为pdf或者jpg的发票文件。

3、我们需要调用百度智能云ocr识别技术(可识别pdf和图片格式文件),识别发票文件中的商品明细:

4、识别发票明细后,将发票明细按照“| 编号 | 材料、易耗品名称 | 规格型号 | 单位 | 数量 | 单价 | 金额 | 备注 |”的格式以markdown语言用表格的形式,将信息存入“发票内容”列中。

5、请注意最终的金额和单价都是含税的。及最终的金额等于发票中的“金额+税额”,单价等于“最终的金额/对应的数量”。
在脚本中输入以下代码:

块引用
import os
import requests
import json
from io import BytesIO
import fitz # PyMuPDF
from PIL import Image
import base64
from urllib.parse import quote
百度智能云OCR配置
BAIDU_API_KEY = ‘I000X’
BAIDU_SECRET_KEY = ‘z0000’
#SeaTable 配置
SEATABLE_CLOUD_URL = “---------------” # 确保以斜杠结尾
USERNAME = “0000” # 替换为SeaTable用户名
PASSWORD = “000” # 替换为SeaTable密码
DTABLE_NAME = “fapiaoguanli”
TABLE_NAME = “Invoices”
DTABLE_UUID = “53b000002d3” # 提供的DTable UUID
def get_account_token(username, password):
url = f"{SEATABLE_CLOUD_URL}api2/auth-token/"
data = {
‘username’: username,
‘password’: password
}
headers = {
‘Content-Type’: ‘application/x-www-form-urlencoded’
}
response = requests.post(url, headers=headers, data=data)
if response.status_code == 200:
return response.json()[‘token’]
else:
raise Exception(f"Failed to get account token: {response.status_code} {response.text}“)
def get_rows_from_seatable(seatable_url, api_token, dtable_uuid, table_name):
url = f”{seatable_url}api/v2.1/dtables/{quote(dtable_uuid)}/rows/?table_name={quote(table_name)}"
headers = {
‘Authorization’: f’Token {api_token}‘,
‘Content-Type’: ‘application/json’
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
return response.json()
else:
raise Exception(f"Failed to get rows from Seatable: {response.status_code} {response.text}“)
def update_row_in_seatable(seatable_url, api_token, dtable_uuid, table_name, row_id, updated_data):
url = f”{seatable_url}api/v2.1/dtables/{quote(dtable_uuid)}/rows/{row_id}/?table_name={quote(table_name)}"
headers = {
‘Authorization’: f’Token {api_token}’,
‘Content-Type’: ‘application/json’
}
response = requests.patch(url, headers=headers, data=json.dumps(updated_data))
if not response.ok:
raise Exception(f"Failed to update row in Seatable: {response.status_code} {response.text}“)
def get_access_token(api_key, secret_key):
url = f"热门问题-技术问答-百度开发者中心-汇聚开放、助力、共赢
response = requests.get(url)
if response.status_code == 200:
return response.json().get(“access_token”)
else:
raise Exception(f"Failed to get access token: {response.status_code} {response.text}“)
def ocr_vat_invoice(image_data, access_token):
url = f"https://aip.baidubce.com/rest/2.0/ocr/v1/vat_invoice?access_token={access_token}
headers = {‘Content-Type’: ‘application/x-www-form-urlencoded’}
data = {
‘image’: image_data,
‘seal_tag’: ‘false’
}
response = requests.post(url, headers=headers, data=data)
if response.status_code == 200:
return response.json()
else:
raise Exception(f"Failed to OCR invoice: {response.status_code} {response.text}")
main()

返回以下信息:

块引用

您这里获取seatable的行的 url 写错了, 您这种情况可以使用 seatable 提供的 python sdk, 可以参考:SeaTable 编程手册