admin管理员组

文章数量:1605151

Python3.6实现图片文字识别和PDF转WORD

  • 图片文字提取
    • - - 需求来源 - -
    • - - 代码实现 - -
  • PDF转WORD
    • - - 需求来源 - -
    • - - 代码实现 - -
  • 窗口实现
    • - - 使用PyQt5创建 - -
    • - - 使用tkinter 创建 - -
  • 最终实现
    • - - 实现说明 - -
    • - - 完整代码(百度AI账号自申)- -
  • 程序打包
    • - - 使用pyinstaller - -

图片文字提取

- - 需求来源 - -

因为朋友抱怨图片提取文字都是限制图片且付费的,于是就想利用Python开源的优势做一个免费的程序,可以实现图片中文字的提取。

- - 代码实现 - -

参考百度图片文字识别过程:
https://blog.csdn/XnCSD/article/details/80786793

忽略图片压缩过程,只识别一张图片

import sys
import os
import glob
from os import path
from aip import AipOcr
from PIL import Image
def baiduOCR(picfile, outfile):
    """利用百度api识别文本,并保存提取的文字
    picfile:    图片文件名
    outfile:    输出文件
    """
    filename = path.basename(picfile)
    
    APP_ID = '***' # 刚才获取的 ID,下同
    API_KEY = '***'
    SECRECT_KEY = '***'
    client = AipOcr(APP_ID, API_KEY, SECRECT_KEY)
    
    i = open(picfile, 'rb')
    img = i.read()
    #print("正在识别图片:\t" + filename)
    message = client.basicGeneral(img)   # 通用文字识别,每天 50 000 次免费
    #message = client.basicAccurate(img)   # 通用文字高精度识别,每天 800 次免费
    #print("识别成功!")
    i.close();
    
    with open(outfile, 'a+') as fo:
        fo.writelines("+" * 60 + '\n')
        fo.writelines("识别图片:\t" + filename + "\n" * 2)
        fo.writelines("文本内容:\n")
        # 输出文本内容
        for text in message.get('words_result'):
            fo.writelines(text.get('words') + '\n')
        fo.writelines('\n'*2)
    #print("文本导出成功!")
    #print()

picfile = 'path of picture' #图片路径 
outfile = 'result.txt'
if path.exists(outfile):
    os.remove(outfile)
#lb.config(text = "图片识别...");
baiduOCR(picfile, outfile)
#lb.config(text = "图片文本提取结束!文本输出结果位于"+outfile+"文件中");

PDF转WORD

- - 需求来源 - -

微软Office和金山WPS都可以免费实现word转pdf,但是PDF转word文档需要会员或者一定费用实现全部pdf的转换。
一些在线的网站以可完成免费的pdf 转 word:
50M以内的PDF转换: 便捷PDF转换器
== 如果只是想提取PDF中的文字(不包含格式),可以使用此程序进行转换。==

- - 代码实现 - -

import sys
import importlib
importlib.reload(sys)
 
from pdfminer.pdfparser import PDFParser,PDFDocument
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from p

本文标签: 文字简单图片软件Python