使用python脚本实现将图片表格转换为excel文档
使用python脚本实现将图片表格转换为excel文档
pip3 install tencentcloud_sdk_python
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
pythonpython3 imgToExcel.py 111.png
本文作者:SnailBoy
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!