吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 10917|回复: 109
收起左侧

[原创工具] TXT文本提取工具-20240204开源

  [复制链接]
wesley1224 发表于 2024-1-5 14:31
本帖最后由 wesley1224 于 2024-2-4 18:05 编辑

朋友叫的需求,目前在自学python中,自写的TXT文本提取小工具,送给用得上的朋友。语言:python.  
功能:把路径下的所有文件夹内的 txt 提取到一个output.txt内,按文件名+内容显示。


PixPin_2024-01-05_14-04-281.jpg
解压密码:52pojie


2024-02-04 同时开源,源码很简单。
PixPin_2024-02-04_17-40-59.jpg
小修正:为了确保输出的内容严格按照.txt文件的顺序排列,不使用线程池进行并发处理了,而是改为顺序处理每个文件。


[Python] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import os
import tkinter as tk
from tkinter import messagebox
from tkinter import ttk
 
def search_txt_files(dir_path):
    return [os.path.join(root, file_name) for root, dirs, files in os.walk(dir_path)
            for file_name in files if file_name.endswith(".txt")]
 
def process_txt(txt_file):
    with open(txt_file, 'r', encoding="utf-8") as txt:
        return f"{os.path.basename(txt_file)} {txt.read()}"
 
def write_txt(dir_path, output_file='output.txt'):
    txt_files = search_txt_files(dir_path)
    processed_contents = []
 
    for txt_file in txt_files:
        try:
            data = process_txt(txt_file)
            processed_contents.append(data)
        except Exception as exc:
            print(f"Failed to process {txt_file}: {exc}")
 
    with open(os.path.join(dir_path, output_file), 'w', encoding="utf-8") as f:
        for content in processed_contents:
            f.write(f"{content}\n")
 
def show_message_box(default_path):
    def validate_and_write():
        user_path = entry.get()
        if os.path.isdir(user_path):
            write_txt(user_path)
        else:
            messagebox.showerror("错误", "请提供有效的目录路径。")
 
    root = tk.Tk()
    root.title("TXT文本提取合成工具 by wesley_net")
    root.geometry('350x200')
 
    label_title = tk.Label(root, text="请输入或粘贴路径", font=("Arial", 20))
    label_title.pack(pady=10)
 
    entry = tk.Entry(root, width=45, justify=tk.LEFT)
    entry.insert(0, default_path)
    entry.pack(pady=10)
 
    button_ok = tk.Button(root, text="确定", command=validate_and_write)
    button_close = tk.Button(root, text="关闭", command=root.quit)
 
    button_ok.place(x=120, y=150, anchor='center', width=70)
    button_close.place(x=190, y=150, anchor='w', width=70)
 
    root.mainloop()
 
current_directory = os.getcwd()
show_message_box(current_directory)


https://wwt.lanzoul.com/b00s259id
密码:5re7

免费评分

参与人数 14吾爱币 +19 热心值 +10 收起 理由
似水流年2015 + 2 + 1 这个可以用在xp上吗
Zatoichi + 1 + 1 谢谢@Thanks!
catoo1 + 1 谢谢@Thanks!
xyyh816 + 1 + 1 谢谢@Thanks!
远水涵秋 + 1 我很赞同!
zkyuan + 1 热心回复!
confiant + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
风之暇想 + 7 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
yanglinman + 1 谢谢@Thanks!
TAYNEW + 1 + 1 谢谢@Thanks!
zanaa007 + 1 + 1 用心讨论,共获提升!
powehi + 1 + 1 谢谢@Thanks!
gmx1222 + 1 + 1 热心回复!
sonc0345 + 1 用心讨论,共获提升!

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

Deepstarry 发表于 2024-3-1 14:07

客气了。另外,我最近在玩讯飞有声。挺不错的一个小应用。如果你也喜欢听书的话。可以直接在里面导入txt、epub、pdf(扫描版不行)等格式。然后,它可以直接朗读。优点有两个:一是可以完全离线使用;另一个是,可以录制模拟指定的人声。效果还不错。
尹铭 发表于 2024-1-5 14:36
sp3bf 发表于 2024-1-5 14:36
jokeen 发表于 2024-1-5 14:38
看起来不错呀,感谢楼主分享
熊有儒 发表于 2024-1-5 14:38
感谢楼主分享原创作品,吾爱有您更精彩
yourbestrun 发表于 2024-1-5 14:43
请问有什么使用场景吗 ?
cq2002 发表于 2024-1-5 14:45
这东西好。多谢分享
Marken888 发表于 2024-1-5 14:52
yourbestrun 发表于 2024-1-5 14:43
请问有什么使用场景吗 ?

可能在需要批量读取txt的时候有用
 楼主| wesley1224 发表于 2024-1-5 14:56
yourbestrun 发表于 2024-1-5 14:43
请问有什么使用场景吗 ?

场景很多 例如:某个系统产生的日志都是在一个目录内的 每天都有一个对应日期的记录运行事情的txt。那么就可以直接合成一个来看。等等
破解专用户 发表于 2024-1-5 15:02
我开始以为批量把SRT转成TXT(当然里面的时间线和数字序号)都要去掉,只要字幕文本……这一看也不是啊,所以不知道使用场景 
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2025-5-28 15:58

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表