小尤学编程 2022-07-25 23:16 采纳率: 33.3%
浏览 62
已结题

请问这是为什么FileNotFoundError: [Errno 2] No such file or directory: 'img_path'

是我从网上看视频跟着人家一步一步敲的,不知道哪里出问题了,图片跟程序在一个目录下面。


import uuid
import requests
import base64
import hashlib
import time
import json

YOUDAO_URL = 'https://openapi.youdao.com/ocr_table'
APP_KEY = '75038d83358839f1'
APP_SECRET = 'XakTRoj1uHPMpltPiR04xw55z7eTrPkF'


def truncate(q):
    if q is None:
        return None
    q_utf8 = q.decode("utf-8")
    size = len(q_utf8)
    return q_utf8 if size <= 20 else q_utf8[0:10] + str(size) + q_utf8[size - 10:size]


def encrypt(signStr):
    hash_algorithm = hashlib.sha256()
    hash_algorithm.update(signStr.encode('utf-8'))
    return hash_algorithm.hexdigest()


def do_request(data):
    headers = {'Content-Type': 'application/x-www-form-urlencoded'}
    return requests.post(YOUDAO_URL, data=data, headers=headers)


def connect(img_path):
    f = open(r'img_path', 'rb')  # 二进制方式打开图文件
    q = base64.b64encode(f.read())  # 读取文件内容,转换为base64编码
    f.close()

    data = {}
    data['type'] = '1'
    data['q'] = q
    data['docType'] = 'excel' # excel相关数据
    data['signType'] = 'v3'
    curtime = str(int(time.time()))
    data['curtime'] = curtime
    salt = str(uuid.uuid1())
    signStr = APP_KEY + truncate(q) + salt + curtime + APP_SECRET
    sign = encrypt(signStr)
    data['appKey'] = APP_KEY
    data['salt'] = salt
    data['sign'] = sign

    # 包含Excel相关数据(base64字符串)   json
    response = do_request(data)
    # json -- 字典
    # 拿到的是包含Excel的base64的json字符串
    response_json = json.loads(response.text)
    # 字典取值
    print(response_json)
    # 提取数据
    excel_table_base64 = response_json.get('Result').get('tables')[0]
    print('转换完成')
    return excel_table_base64

def orc_excel(img_path,excel_path):
    excel_table_base64 = connect(img_path)
    # 将数据解码
    decoded = base64.b64decode(excel_table_base64)

    # 将解码后的数据写入Excel文件
    with open(excel_path,'wb') as f:
        f.write(decoded)
        print('保存成功')


if __name__ == '__main__':
    img_path = r'./table.jpg'
    excel_path = r'./file.xlsx'
    # 获取数据并且转换后---保存数据
    orc_excel(img_path,excel_path)

img

  • 写回答

4条回答 默认 最新

  • 快乐小土狗 2022-07-25 23:51
    关注

    这里应该是引用的变量img_path
    把r’‘去掉

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 8月3日
  • 已采纳回答 7月26日
  • 创建了问题 7月25日

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程