编辑
2023-08-23
开发
00
请注意,本文编写于 249 天前,最后修改于 249 天前,其中某些信息可能已经过时。

目录

步骤一,开通腾讯云的【通用文字识别服务】
步骤二、申请一个apiKey
步骤三 安装腾讯的 python库
步骤四 建立脚本
步骤五 执行脚本见证奇迹

使用python脚本实现将图片表格转换为excel文档

使用python脚本实现将图片表格转换为excel文档

image.png

步骤一,开通腾讯云的【通用文字识别服务】

image.png

步骤二、申请一个apiKey

image.png

步骤三 安装腾讯的 python库

pip3 install tencentcloud_sdk_python

image.png

步骤四 建立脚本

imgToExcel.py

python
#coding=utf-8 import sys import ocr import yaml def get_yaml_data(yaml_file): # 打开yaml文件 file = open(yaml_file, 'r', encoding="utf-8") file_data = file.read() file.close() # 将字符串转化为字典或列表 data = yaml.load(file_data, Loader=yaml.FullLoader) return data def imageToExcel(pic_path): config = get_yaml_data("config.yml") # 使用ocr进行转换 trans = ocr.OCR() path_excel = trans.img_to_excel( pic_path, image_path=pic_path, secret_id=config['secret_id'], secret_key=config['secret_key'], ) if __name__ == '__main__': pic_path = sys.argv[1] imageToExcel(pic_path)

ocr.py

python
#coding=utf-8 from tencentcloud.common import credential from tencentcloud.common.profile.client_profile import ClientProfile from tencentcloud.common.profile.http_profile import HttpProfile from tencentcloud.ocr.v20181119 import ocr_client, models import base64 # OCR识别封装 class OCR(object): def img_to_excel(self, output_file_name, image_path, secret_id, secret_key): # 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey cred = credential.Credential( secret_id, secret_key ) # 实例化client对象 httpProfile = HttpProfile() httpProfile.endpoint = "ocr.tencentcloudapi.com" clientProfile = ClientProfile() clientProfile.httpProfile = httpProfile clientProfile.signMethod = "TC3-HMAC-SHA256" client = ocr_client.OcrClient(cred, "ap-shanghai", clientProfile) # 实例化一个请求对象 #req = models.GeneralFastOCRRequest() req = models.GeneralFastOCRRequest() # 读取图片数据,使用Base64编码 with open(image_path, 'rb') as f: image = f.read() image_base64 = str(base64.b64encode(image), encoding='utf-8') req.ImageBase64 = image_base64 # 通过client对象调用访问接口,传入请求对象 resp = client.RecognizeTableOCR(req) # 获取返回数据(Data为Base64编码后的Excel数据) data = resp.Data # 转换为Excel output_file_name = str(output_file_name) path_excel = output_file_name + ".xlsx" with open(path_excel, 'wb') as f: f.write(base64.b64decode(data)) return path_excel

config.yml 替换你自己的apikey信息

#coding=utf-8 secret_id: a secret_key: a

步骤五 执行脚本见证奇迹

python
python3 imgToExcel.py 111.png

image.png

本文作者:SnailBoy

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!