本帖最后由 d8349565 于 2020-12-5 00:35 编辑
“专业的IT人员不做繁琐的日常文件管理工作,专业的白领也负担不起软件定制与维护”
以上大概就是我开始自学Python编程的原因吧!
-----------------------------------------------------------------------------------------------------------------------------------------------
软件介绍:
很久没更新,大概就是因为标题中指的时不时一大堆合同给耽误了吧。
日常工作中,确实有很多类似于合同、协议、表单等,在扫描完成后,需要进行归档存储。大部分的扫描仪都只能帮你自动按照时间流水号,相信很多人都痛苦与扫描后一个个打开手动重命名的工作了。
所以,我临时搜索自学了下,制作了以下软件。
使用方法:
1、以下为举例的扫描后需要重命名存档的PDF文件:
2、还好每个文件都有一份固定格式的评审表(虚拟的),通过代码识别合同名称、合同签订方后面的文本内容:
3、双击打开软件,示例如下:
4、特别注意:
由于代码中调用了百度文本识别的API,高精度识别版本,每天免费调用500次,可以到以下网址进行申请“申请应用获取的 ID,下同 链接:https://ai.baidu.com/tech/ocr/general”
每个人的文件内容都不一样,请在根目录下的txt中修改关键字1和关键字2
5、代码分享
[Python] 纯文本查看 复制代码 '''
文件扫描自动重命名
@Auther Lee
@software: Pycharm
@Version:1.0
'''
import easygui as g
from PyPDF2 import PdfFileReader,PdfFileWriter # 读和写
import fitz # fitz pip install PyMuPDF
import os
from aip import AipOcr
import re
def get_files(path):
pdf_files=list(os.scandir(path))
return pdf_files
def get_page1(pdf_file):
read_PDF = PdfFileReader(pdf_file)
write_pdf = PdfFileWriter() # 实例化对象
write_pdf.addPage(read_PDF.getPage(0)) # 将遍历出的每一页添加到实例化对象中
pdf_path=f'{os.path.dirname(pdf_file)}\\临时.pdf'
with open(pdf_path, "wb") as b:
write_pdf.write(b)
return pdf_path
def pyMuPDF_fitz(pdfPath):
imagePath=os.path.dirname(pdfPath)
pdfDoc = fitz.open(pdfPath)
page = pdfDoc[0]
rotate = int(0)
# 每个尺寸的缩放系数为1.3,这将为我们生成分辨率提高2.6的图像。
# 此处若是不做设置,默认图片大小为:792X612, dpi=96
zoom_x = 1.33333333 # (1.33333333-->1056x816) (2-->1584x1224)
zoom_y = 1.33333333
mat = fitz.Matrix(zoom_x, zoom_y).preRotate(rotate)
pix = page.getPixmap(matrix=mat, alpha=False)
if not os.path.exists(imagePath): # 判断存放图片的文件夹是否存在
os.makedirs(imagePath) # 若图片文件夹不存在就创建
png_path = imagePath + '\\临时.png'
pix.writePNG(png_path) # 将图片写入指定的文件夹内
return png_path
def baiduOCR(picfile):
# APP_ID = '自行填写' # 申请应用获取的 ID,下同 链接:https://ai.baidu.com/tech/ocr/general
# API_KEY = '自行填写'
# SECRECT_KEY = '自行填写'
client = AipOcr(APP_ID, API_KEY, SECRECT_KEY)
image = open(picfile, 'rb')
rb_data = image.read()
message = client.basicAccurate(rb_data) # 通用文字高精度识别,每天 500 次免费
image.close()
return message
def rename_pdf(pdf_file_path,a,b):
new_path=f'{os.path.dirname(pdf_file_path)}\\{a}-{b}.pdf'
os.rename(pdf_file_path,new_path)
return new_path
def main():
path=g.diropenbox(msg='请选择需要处理的文件夹') # 获取文件夹路径
pdf_files=get_files(path) # 获取所有文件
for i in range(0,len(pdf_files)):
pdf_file=pdf_files[i] # 获取第n个文件
file_path=os.path.abspath(pdf_file) # 获取此文件的路径
try:
page1_path=get_page1(file_path) # 将PDF文件的第一页提取并保存为《临时.pdf》 返回绝对路径
except:
print(f'失败:将{pdf_file}的第一页提取并保存为《临时.pdf》')
continue
try:
png_file=pyMuPDF_fitz(page1_path) # 将《临时.pdf》转换为《临时.png》 返回绝对路径
except:
print(f'失败:将{pdf_file}的《临时.pdf》转换为《临时.png》')
continue
message=baiduOCR(png_file) # 通过百度ocr识别《临时.png》中的文本内容
content=message.get('words_result') # 筛选出文本内容部分
The_text=[i.get('words') for i in content] # 筛选出文本内容部分为列表
choices=[] # 设置一个空列表
try:
for i in The_text:
if 关键字1 in i or 关键字2 in i: # 如果文本中有此关键字,则进行以下内容
i = i.split(':')[-1] # 取冒号右侧内容
cop = re.compile("[^\u4e00-\u9fa5^a-z^A-Z^0-9]") # 匹配不是中文、大小写、数字的其他字符
i=cop.sub('', i) # 将匹配到的字符替换成空字符
choices.append(i) # 添加到空列表中
except:
print(f'失败:{pdf_file}无此关键内容')
continue
try:
new_path=rename_pdf(file_path,choices[0],choices[1]) # 通过关键字筛选出的文本来重命名文件
print(f'成功:{new_path}已完成重命名')
except:
print(f'失败:{pdf_file}无法重命名')
continue
try:
os.remove(page1_path) # 删除临时文件《临时.pdf》
os.remove(png_file) # 删除临时文件《临时.png》
except:
continue
if __name__ == '__main__':
with open ('信息补充.txt','r',encoding='utf-8') as t:
txt=t.readlines()
# print(txt)
APP_ID = txt[0].split("=")[-1].strip()
API_KEY = txt[1].split("=")[-1].strip()
SECRECT_KEY = txt[2].split("=")[-1].strip()
关键字1=txt[3].split("=")[-1].strip()
关键字2=txt[4].split("=")[-1].strip()
# print(APP_ID,API_KEY,SECRECT_KEY,关键字1,关键字2)
main()
6、exe文件分享
蓝奏云:https://leezfy.lanzouj.com/in83mj26jqh 密码:c43y
|