用发票金额重命名pdf发票文件

查看 83|回复 9
作者:lzmomo   
python新手写的小工具,代码质量有欠缺,但能运行。
里面有些地方处理得不够简洁,希望得到大家的指点。
用发票金额重命名pdf发票文件
用发票金额重命名文件,在发票文件的目录下会新建一个‘重命名文件’的文件夹,存放修改后的文件。
步骤:运行程序,选择PDF文档,选择发票。
代码如下:
[Python] 纯文本查看 复制代码import pdfplumber
import re
import tkinter as tk
from tkinter import filedialog
import threading
import shutil
import os
import configparser
# 新线程的工作函数
def worker(arg):
    # 创建ConfigParser对象
    config = configparser.ConfigParser()
    # 读取INI文件
    config.read('config.ini')
    xuhao = config.get('RunCount1', 'count1')
    oldfile = config.get('RunCount2', 'count2')
    oldfile_data = os.path.dirname(oldfile)
    money = config.get('RunCount2', '金额')
    newfile = oldfile_data+'/重命名文件/'+money+'('+xuhao+').pdf'
    # 检查重命名文件文件夹是否存在
    if not os.path.exists(os.path.join(oldfile_data, '重命名文件')):
        # 如果不存在,就创建重命名文件文件夹
        os.makedirs(os.path.join(oldfile_data, '重命名文件'))
        print('重命名文件夹创建成功')
        
    shutil.move(oldfile, newfile)   # move是移动,如果只想复制可以更换为copy
def main():
    filetypes = (
        ('pdf文件', '*.pdf'),
        ('所有文件', '*.*')
    )
    filename = filedialog.askopenfilename(
        title='选择要打开的文件',
        initialdir='/',
        filetypes=filetypes)
    if filename:
        # 读取PDF文档
        with pdfplumber.open(filename) as pdf:
            # 获取文档的总页数
            total_pages = len(pdf.pages)
            # 遍历每一页
            for page_number in range(total_pages):
                # 获取当前页
                page = pdf.pages[page_number]
                # 提取文本内容
                text = page.extract_text()
                pattern = r'(小写)¥\d+\.\d+'
                # 使用re.search 方法提取匹配的内容
                match = re.search(pattern, text)
                money = match.group()
                text = money
                pattern = r"(\d+(\.\d+)?)"
                result = re.search(pattern, text)
                extracted_amount = result.group()
                money = extracted_amount
                # 检查是否找到匹配项并打印结果
                if match:
                    # 要写入的新内容
                    new_lines = [filename, money]
                    # 创建配置文件对象
                    config = configparser.ConfigParser()
                    # 设置运行次数的节和键
                    run_count_section = 'RunCount1'
                    run_count_key = 'count1'
                    run_count_section_1 = 'RunCount2'
                    run_count_key_1 = 'count2'
                    # 检查配置文件是否存在,如果不存在则创建
                    config_file = 'config.ini'
                    if not config.read(config_file, encoding='ANSI'):
                        config[run_count_section] = {run_count_key: '0'}
                        with open(config_file, 'w', encoding='ANSI') as configfile:
                            config.write(configfile)
                    config[run_count_section_1] = {run_count_key_1: filename,
                                                   '金额': money
                                                   }
                    # 读取运行次数
                    current_count = config.getint(run_count_section, run_count_key)
                    # 递增运行次数
                    new_count = current_count + 1
                    config.set(run_count_section, run_count_key, str(new_count))
                    # 保存配置文件
                    with open(config_file, 'w', encoding='ANSI') as configfile:
                        config.write(configfile)
                    print(f"重命名的第: {new_count}张发票", '金额:', money)
                    section1_values_1 = config['RunCount1']
                    section1_values_2 = config['RunCount2']
                    list_data = ['发票信息',
                                 section1_values_2['count2'],
                                 os.path.dirname(section1_values_2['count2']),
                                 '/',
                                 section1_values_2['金额'],
                                 '(',
                                 section1_values_1['count1'],
                                 ').pdf']
                    # 主线程中创建新线程,并传递参数
                    t = threading.Thread(target=worker, args=(list_data,))
                    t.start()
                else:
                    print("未找到匹配项")
# 程序入口
if __name__ == "__main__":
    root = tk.Tk()
    root.title("文件上传")
    root.geometry('300x150')
    upload_button = tk.Button(root, text='选择PDF文档', command=main)
    upload_button.pack(expand=True)
    root.mainloop()

文件, 重命名

y25131492011103   

巧了  每天给客户开的发票都命名发票金额    楼主能发成品么  存小白不会操作
小小小酥   

感谢大佬分享
xueyinglantian   

谢谢楼主分享,对于小白新人来谁,这个复制代码以后,我要怎么弄啊,感觉很需要,谢谢
Xiaosesi   

非常方便好用,节省了手动重名的时间
zylz9941   

如果能够再加上购买方名称就更完美了
hfhskf2005   

财务人员的好工具啊
mrpizi1221   

感谢分享,学习使我快乐。哈哈哈
lzmomo
OP
  


hfhskf2005 发表于 2024-8-9 10:42
财务人员的好工具啊

我也是想给自己偷个懒
lzmomo
OP
  


xueyinglantian 发表于 2024-8-9 10:25
谢谢楼主分享,对于小白新人来谁,这个复制代码以后,我要怎么弄啊,感觉很需要,谢谢

把python环境弄好,运行就行了。
您需要登录后才可以回帖 登录 | 立即注册

返回顶部