from aip import AipOcr
import os


def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()


def put_file_content(filePath, content):
    with open(filePath, 'w', encoding='utf-8') as fp:
        fp.write(content)


APP_ID = ''
API_Key = ''
Secret_Key = ''
client = AipOcr(APP_ID, API_Key, Secret_Key)  # 创建AipOcr对象
folderPath = './image/'  # 设置图片存放文件夹路径
img_list = os.listdir(folderPath)  # 返回图片文件中的图片文件名列表
strAll = ''  # 存放各文件识别的合并结果
pageNum = 0  # 对应图片的页码
for img in img_list:
    pageNum += 1
    strFile = str(pageNum) + '#' * 60 + '\n'
    filePath = folderPath + img
    result = client.basicGeneral(get_file_content(filePath))
    for strLine in result["words_result"]:  # 遍历文字识别结果
        strFile = strFile + strLine["words"] + '\n'
    strAll += strFile + '\n'  # 将识别文字添加到合并结果strAll中
put_file_content('result.txt', strAll)  # 将合并结果输出到文本文件
os.system('notepad result.txt')  # 打开记事本显示合并结果
