Python制作一个简单的图片文字提取+PDF转WORD的软件

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

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

图片文字提取

- - 需求来源 - -

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

- - 代码实现 - -

参考百度图片文字识别过程:
https://blog.csdn.net/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 pdfminer.converter import PDFPageAggregator
from pdfminer.layout import *
from pdfminer.pdfinterp import PDFTextExtractionNotAllowed
 
'''
解析pdf文件,获取文件中包含的各种对象
'''
 
# 解析pdf文件函数
def parse(pdf_path):
    fp = open(pdf_path, 'rb')  # 以二进制读模式打开
    # 用文件对象来创建一个pdf文档分析器
    parser = PDFParser(fp)
    # 创建一个PDF文档
    doc = PDFDocument()
    # 连接分析器 与文档对象
    parser.set_document(doc)
    doc.set_parser(parser)
 
    # 提供初始化密码
    # 如果没有密码 就创建一个空的字符串
    doc.initialize()
 
    # 检测文档是否提供txt转换,不提供就忽略
    if not doc.is_extractable:
        raise PDFTextExtractionNotAllowed
    else:
        # 创建PDf 资源管理器 来管理共享资源
        rsrcmgr = PDFResourceManager()
        # 创建一个PDF设备对象
        laparams = LAParams()
        device = PDFPageAggregator(rsrcmgr, laparams=laparams)
        # 创建一个PDF解释器对象
        interpreter = PDFPageInterpreter(rsrcmgr, device)
 
        # 用来计数页面,图片,曲线,figure,水平文本框等对象的数量
        num_page, num_image, num_curve, num_figure, num_TextBoxHorizontal = 0, 0, 0, 0, 0
 
        # 循环遍历列表,每次处理一个page的内容
        for page in doc.get_pages(): # doc.get_pages() 获取page列表
            num_page += 1  # 页面增一
            interpreter.process_page(page)
            # 接受该页面的LTPage对象
            layout = device.get_result()
            for x in layout:
                if isinstance(x,LTImage):  # 图片对象
                    num_image += 1
                if isinstance(x,LTCurve):  # 曲线对象
                    num_curve += 1
                if isinstance(x,LTFigure):  # figure对象
                    num_figure += 1
                if isinstance(x, LTTextBoxHorizontal):  # 获取文本内容
                    num_TextBoxHorizontal += 1  # 水平文本框对象增一
                    # 保存文本内容
                    with open(r'test.doc', 'a',encoding='utf-8') as f:    #生成doc文件的文件名及路径
                        results = x.get_text()
                        f.write(results)
                        f.write('\n')
        print('对象数量:\n','页面数:%s\n'%num_page,'图片数:%s\n'%num_image,'曲线数:%s\n'%num_curve,'水平文本框:%s\n'
              %num_TextBoxHorizontal)
 
if __name__ == '__main__':
    pdf_path = r'test.pdf'  #pdf文件路径及文件名
    parse(pdf_path)

参考PDF转WORD过程:
https://blog.csdn.net/Initiallysunny/article/details/79960838

窗口实现

- - 使用PyQt5创建 - -

PyQt5 创建文件选择窗口
最开始了解到 PyQt5 在最近的Python图形界面实现领域较为流行,就采用了PyQt5。
但在最终的打包实现阶段出现了问题,即使在自己的电脑上打包成功,也无法在其他电脑上成功运行。详见 PyQt5打包失败建议使用tkinter
PyQt5创建文件选择窗口参考代码:
https://blog.csdn.net/humanking7/article/details/80546728

- - 使用tkinter 创建 - -

代码实现:

from tkinter import *
import tkinter.filedialog
windows = tkinter.Tk() #创建主窗口
windows.title("功能选择") #设置窗口标题
windows.geometry("400x120") #设置窗口大小
'''
活动代码部分
'''
windows.mainloop() #程序窗口循环

因为tkinter是Python自带安装的库,所以打包时可以直接pyinstall -F -w #.py

最终实现

- - 实现说明 - -

窗口负责完成文件的选择,包括图片和PDF文件。
Python制作一个简单的图片文字提取+PDF转WORD的软件_第1张图片
将图片文字识别和PDF转WORD的函数,关联窗口的按钮响应,文件确定后进行相应的操作。

- - 完整代码(百度AI账号自申)- -

import sys
import os
from tkinter import *
import tkinter.filedialog

import glob
from os import path
from aip import AipOcr
from PIL import Image

import importlib
importlib.reload(sys)
 
from pdfminer.pdfparser import PDFParser,PDFDocument
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import PDFPageAggregator
from pdfminer.layout import *
from pdfminer.pdfinterp import PDFTextExtractionNotAllowed

root = Tk() #创建主窗口
root.title("功能选择") #设置窗口标题
root.geometry("400x120")
    
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()

 
# 解析pdf文件函数
def parse(pdf_path):
    fp = open(pdf_path, 'rb')  # 以二进制读模式打开
    # 用文件对象来创建一个pdf文档分析器
    parser = PDFParser(fp)
    # 创建一个PDF文档
    doc = PDFDocument()
    # 连接分析器 与文档对象
    parser.set_document(doc)
    doc.set_parser(parser)
 
    # 提供初始化密码
    # 如果没有密码 就创建一个空的字符串
    doc.initialize()
 
    # 检测文档是否提供txt转换,不提供就忽略
    if not doc.is_extractable:
        raise PDFTextExtractionNotAllowed
    else:
        # 创建PDf 资源管理器 来管理共享资源
        rsrcmgr = PDFResourceManager()
        # 创建一个PDF设备对象
        laparams = LAParams()
        device = PDFPageAggregator(rsrcmgr, laparams=laparams)
        # 创建一个PDF解释器对象
        interpreter = PDFPageInterpreter(rsrcmgr, device)
 
        # 用来计数页面,图片,曲线,figure,水平文本框等对象的数量
        num_page, num_image, num_curve, num_figure, num_TextBoxHorizontal = 0, 0, 0, 0, 0
 
        # 循环遍历列表,每次处理一个page的内容
        for page in doc.get_pages(): # doc.get_pages() 获取page列表
            num_page += 1  # 页面增一
            interpreter.process_page(page)
            # 接受该页面的LTPage对象
            layout = device.get_result()
            for x in layout:
                if isinstance(x,LTImage):  # 图片对象
                    num_image += 1
                if isinstance(x,LTCurve):  # 曲线对象
                    num_curve += 1
                if isinstance(x,LTFigure):  # figure对象
                    num_figure += 1
                if isinstance(x, LTTextBoxHorizontal):  # 获取文本内容
                    num_TextBoxHorizontal += 1  # 水平文本框对象增一
                    # 保存文本内容
                    with open(r'test.doc', 'a',encoding='utf-8') as f:    #生成doc文件的文件名及路径
                        results = x.get_text()
                        f.write(results)
                        f.write('\n')
        print('对象数量:\n','页面数:%s\n'%num_page,'图片数:%s\n'%num_image,'曲线数:%s\n'%num_curve,'水平文本框:%s\n'
              %num_TextBoxHorizontal)

def pic():
    picfile = tkinter.filedialog.askopenfilename(initialdir='C',title='选择图片',filetypes=(("所有图片",("*.jpg","*.jpeg","*.png")),('All file','*.*')))

    if picfile != '':
        lb.config(text = "开始文字识别处理...");

        outfile = 'result.txt'
        if path.exists(outfile):
            os.remove(outfile)

        lb.config(text = "图片识别...");
        baiduOCR(picfile, outfile)
        lb.config(text = "图片文本提取结束!文本输出结果位于"+outfile+"文件中");
        
    else:
        lb.config(text = "您未选择文件!");

def pdf():
    file = tkinter.filedialog.askopenfilename(initialdir='C',title='选择PDF文件',filetypes=(("PDF文件",("*.pdf")),('All file','*.*')))
    if file != '':
        parse(file)
        lb1.config(text = "图片文本提取结束!文本输出结果位于test.doc文件中");
    else:
        lb1.config(text = "您未选择文件!");
lb = Label(root,text = '')
lb.pack()
btn1 = Button(root,text='图片识别文字提取',command = pic)
btn1.pack()
lb1 = Label(root,text = '')
lb1.pack()
btn2 = Button(root,text='PDF转WORD文档',command = pdf)
btn2.pack()
root.mainloop()

程序打包

- - 使用pyinstaller - -

  1. 将源代码和程序需要的第三方库放置在同一个文件夹下,准备打包。
    命令行pip安装的第三方库位于 *:/Python/Lib/site-packages/文件夹下
    Anaconda 安装的第三方库位于 *:/Anaconda/Lib/site-pacbkages/文件夹下
    然后将使用到的第三方库相应的文件夹复制到和源码同一个文件夹下面。
    Python制作一个简单的图片文字提取+PDF转WORD的软件_第2张图片
  2. 进入命令行,进入创建的文件夹下面开始打包。
    Python制作一个简单的图片文字提取+PDF转WORD的软件_第3张图片
  3. 打包完成,程序会在dist文件夹下面 newform.exe
    Python制作一个简单的图片文字提取+PDF转WORD的软件_第4张图片
    Python制作一个简单的图片文字提取+PDF转WORD的软件_第5张图片
    ** 双击 newform.exe 就可以运行程序,还可以发送给好友。**

你可能感兴趣的:(Python制作一个简单的图片文字提取+PDF转WORD的软件)