Python识别拖放的PDF文件再转成文本文件

日常工作中经常用到PDF文件,有些PDF文件的文字是不能复制的,为了复制这些文字,我们需要转化PDF文件,或者采用微信的OCR图片识别文字,这样非常不方便。为此,我编写了一个Python小程序,利用Tkinter的拖放小窗口供平时拖放PDF文件到里面,等待paddle ocr识别PDF图片的文字,再转化为TXT文件,启动系统自带的记事本打开,复制要复制的文字后,再关闭记事本,就可以关闭Python小程序了,当然也可以一直拖放,一直关闭记事本。(不关闭记事本而关闭程序会出错)

运行效果:

原代码:

# -*- coding: utf-8 -*-
"""
Created on Sun Aug 25 10:42:39 2024

@author: YBK
"""

import tkinter as tk
import windnd
from tkinter.messagebox import showinfo
import os

import fitz
from fitz import Document as openPDF
import time
import re
from paddleocr import PaddleOCR
import subprocess

def dec_to_36(num):
    base = [str(x) for x in range(10)] + [chr(x) for x in range(ord('A'),ord("A")+26)]
    # 前者把 0 ~ 9 转换成字符串存进列表 base 里,后者把 A ~ Z 存进列表
    l = []
    if num<0:
        return "-"+dec_to_36(abs(num))
    while True:
        num,rem = divmod(num,36) # 求商 和 留余数
        l.append(base[rem])
        if num == 0:
            return "".join(l[::-1])
        
def nowtime_to_str():
    #将当前时间戳转化为36进制,约6位字符,减少文件名长度
    unix_timestamp = int(time.time())
    return(dec_to_36(unix_timestamp))

def pdf2pic(path, pic_path):
    '''
    # 从pdf中提取图片
    :param path: pdf的路径
    :param pic_path: 图片保存的路径
    :return:
    '''
    t0 = time.perf_counter()
    # 使用正则表达式来查找图片
    checkXO = r"/Type(?= */XObject)"
    checkIM = r"/Subtype(?= */Image)"

    # 打开pdf
    doc = openPDF(path)
    # 图片计数
    imgcount = 0
    lenXREF = doc.xref_length()

    # 打印PDF的信息
    print("文件名:{}, 页数: {}, 对象: {}".format(path, len(doc), lenXREF - 1))

    # 遍历每一个对象
    for i in range(1, lenXREF):
        # 定义对象字符串
        text = doc.xref_object(i)
        isXObject = re.search(checkXO, text)
        # 使用正则表达式查看是否是图片
        isImage = re.search(checkIM, text)
        # 如果不是对象也不是图片,则continue
        if not isXObject or not isImage:
            continue
        imgcount += 1
        # 根据索引生成图像
        pix = fitz.Pixmap(doc, i)
        # 根据pdf的路径生成图片的名称
        # new_name = path.replace('\\', '_') + "_img{}.png".format(imgcount)
        # new_name = new_name.replace(':', '')
        new_name = os.path.basename(path).replace('.pdf', '_') + "img" + str(imgcount).zfill(3) + ".png"
        # 如果pix.n<5,可以直接存为PNG
        if pix.n < 5:
            pix._writeIMG(os.path.join(pic_path, new_name),1,10)
        # 否则先转换CMYK
        else:
            pix0 = fitz.Pixmap(fitz.csRGB, pix)
            pix0._writeIMG(os.path.join(pic_path, new_name),1,10)
            pix0 = None
        # 释放资源
        pix = None
        t1 = time.perf_counter()
        print("运行时间:{}s".format(t1 - t0))
        print("提取了{}张图片".format(imgcount))
def get_file_size(file_path):
    # 获取文件的大小(单位为字节)
    file_size = os.stat(file_path).st_size
    return file_size
def dragged_files(files):
    fileurl = ''
    if len(files) > 1:
        # print("请拖放一个文件!")
        showinfo("提示","请拖放一个文件!")
    else:
        print(files[0].decode('gbk'))
        fileurl = files[0].decode('gbk')
        print(os.path.splitext(fileurl)[1])
    if fileurl != '' and os.path.splitext(fileurl)[1] == '.pdf':
        pdfpath = fileurl
        filename0 = os.path.basename(fileurl).replace('.pdf','') + nowtime_to_str()
        # filename0 用于生成文件夹和文件名,为了不重复,在后面加入编码后的时间戳
        pic_path = f'e:\\临时文件夹\\{filename0}\\'
        if not os.path.exists(pic_path):
            os.mkdir(pic_path)
        m = pdf2pic(pdfpath, pic_path)
        pngpath = pic_path
        outtxtpath = 'e:\\临时文件夹\\'+filename0+'.txt'
        ocr = PaddleOCR(use_angle_cls=True, lang="ch") # need to run only once to download and load model into memory
        lines = []
        for filename in os.listdir(pngpath):
            img_path = pngpath+filename
            result = ocr.ocr(img_path, cls=True)
            print(img_path)
            # image = Image.open(img_path).convert('RGB')
            if result[0] is not None:
                boxes = [detection[0] for line in result for detection in line] # Nested loop added
                txts = [detection[1][0] for line in result for detection in line] # Nested loop added
                scores = [detection[1][1] for line in result for detection in line] # Nested loop added
                for box, txt, score in zip(boxes, txts, scores):
                    if score > 0.75:
                        # lines.append(txt.replace('\n',''))
                        lines.append(txt+'\n')
                lines.append('\n')
        with open(outtxtpath, 'w', encoding='utf-8') as f:
            f.writelines(line for line in lines)
        subprocess.run(['notepad.exe', outtxtpath], check=True)

 
if __name__ == '__main__':
    rootWindow = tk.Tk()
    rootWindow.title("拖放PDF文件识别文字")
    rootWindow.geometry("300x120")
    windnd.hook_dropfiles(rootWindow , func=dragged_files)
    rootWindow.mainloop()

这里面我有个得意之作,就是将时间戳转化为36进制的6位字符,用了避免重复文件名。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值