陕西网站建设公司找哪家网站开发是无形资产
web/
2025/9/30 7:07:57/
文章来源:
陕西网站建设公司找哪家,网站开发是无形资产,网站案例 中企动力技术支持,oppo应用商店下载官方apppython调用百度ai将图片识别为表格excel 表格文字识别(异步接口)图片转excel 表格文字识别V2图片/pdf转excel通用 表格文字识别(异步接口)
图片转excel
百度ai官方文档#xff1a;https://ai.baidu.com/ai-doc/OCR/Ik3h7y238 使用的是表格文字识别(异步接口)#xff0c;同步… python调用百度ai将图片识别为表格excel 表格文字识别(异步接口)图片转excel 表格文字识别V2图片/pdf转excel通用 表格文字识别(异步接口)
图片转excel
百度ai官方文档https://ai.baidu.com/ai-doc/OCR/Ik3h7y238 使用的是表格文字识别(异步接口)同步接口已经下线 import requests
import json
import base64
import time文档https://ai.baidu.com/ai-doc/OCR/Ik3h7y238
# 获取access_token地址https://console.bce.baidu.com/ai/#/ai/ocr/app/list
def get_access_token():client_id xxxxxxxxxxxxxxxxxx # 你的apikeyclient_secret xxxxxxxxxxxxxxxxxxxxxx # 你的Secret Keyhost https://aip.baidubce.com/oauth/2.0/token?grant_typeclient_credentialsclient_id{}client_secret{}.format(client_id, client_secret)response requests.get(host).textdata json.loads(response)access_token data[access_token]return access_token# 获取识别结果
def get_info(access_token):request_url https://aip.baidubce.com/rest/2.0/solution/v1/form_ocr/request# 二进制方式打开图片文件f open(1.jpg, rb)img base64.b64encode(f.read()) # base64编码params {image: img}request_url request_url ?access_token access_tokenheaders {content-type: application/x-www-form-urlencoded}response requests.post(request_url, dataparams, headersheaders)# if response:# print(response.json())data_1 response.json()return data_1# 获取exceldef get_excel(requests_id, access_token):headers {content-type: application/x-www-form-urlencoded}pargams {request_id: requests_id,result_type: excel}url https://aip.baidubce.com/rest/2.0/solution/v1/form_ocr/get_request_resulturl_all url ?access_token access_tokenres requests.post(url_all, headersheaders, paramspargams) # 访问链接获取excel下载页info_1 res.json()[result][ret_msg]excel_url res.json()[result][result_data]excel_1 requests.get(excel_url).contentprint(excel_1)with open(识别结果.xls, wb) as f:f.write(excel_1)print(info_1)def main():print(正在处理中请稍后)access_token get_access_token()data_1 get_info(access_token)try:requests_id data_1[result][0][request_id]if requests_id ! :print(识别完成)except:print(识别错误)print(正在获取excel)time.sleep(10) # 延时十秒让网页图片转excel完毕excel量多的话转化会慢可以延时长一点get_excel(requests_id, access_token)main()
表格文字识别V2
图片/pdf转excel通用
import requests
import json
import base64CLIENT_ID xxxxxxxxxxxxxxxxx # 你的apikey,需要修改
CLIENT_SECRET xxxxxxxxxxxxxxxxxxxxx # 你的Secret Key需要修改# 获取access_token
def get_access_token():auth_url https://aip.baidubce.com/oauth/2.0/tokenparams {grant_type: client_credentials,client_id: CLIENT_ID,client_secret: CLIENT_SECRET,}response requests.post(auth_url, dataparams)data response.json()access_token data.get(access_token)if not access_token:raise 请输入正确的client_id 和 client_secretreturn access_tokendef save_excel(b64_excel, excel_name):# 将base64编码的excel文件解码并保存为本地文件excel base64.b64decode(b64_excel)with open(excel_name, wb) as f:f.write(excel)def to_excel(file_path, excel_name):access_token get_access_token()request_url https://aip.baidubce.com/rest/2.0/ocr/v1/table# 以二进制方式打开图片文件并将其转换为base64编码with open(file_path, rb) as f:file base64.b64encode(f.read())ext file_path.split(.)[-1]if ext in [jpg, jpeg, png, bmp]:# 图片格式data {image: file,return_excel: true,}elif ext pdf:# pdf格式data {pdf_file: file,return_excel: true,}headers {content-type: application/x-www-form-urlencoded}# 发送POST请求进行表格文字识别response requests.post(request_url, params{access_token: access_token}, datadata, headersheaders)if response.ok:data response.json()# 将返回的excel文件保存到本地save_excel(data.get(excel_file, ), excel_name)print(转换完成)else:print(转换失败)if __name__ __main__:img_path 1.png # 要转换的图片文件名pdf_path table.pdf # 要转换的pdf文件名to_excel(file_pathimg_path, excel_nameout_pic.xlsx) # 转换后的excel文件名to_excel(file_pathpdf_path, excel_nameout_pdf.xlsx) # 转换后的excel文件名
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/84323.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!