吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 11640|回复: 100
收起左侧

[原创工具] 【论坛互动案例1】TXT文本批量替换软件

  [复制链接]
d8349565 发表于 2022-2-25 12:52
本帖最后由 d8349565 于 2022-4-12 16:39 编辑

“专业的IT人员不做繁琐的日常文件管理工作,专业的白领也负担不起软件定制与维护”
    以上大概就是我开始自学Python编程的原因吧!
-----------------------------------------------------------------------------------------------------------------------------------------------


软件介绍
       看到论坛上的小伙伴悬赏一个【将文件名和文件内文本的特定词句(连续的)全部替换为指定词句】的工具“寻一款TXT文本处理软件,需要具备以下功能https://www.52pojie.cn/thread-1592239-1-1.html(出处: 吾爱破解论坛)“,
于是尝试使用python制作了以下软件。

功能介绍
      批量替换txt文件中的指定内容,同时也可以替换修改文件名。


界面:
image.png

操作指导:



下载地址
[color=rgba(0, 0, 0, 0.85)]「TXT文本批量替换软件」https://www.aliyundrive.com/s/gFVDKhzGwaM 提取码: 4oe6

点评

懒揍云:https://l13144.lanzoul.com/iYRtl00megud  发表于 2022-2-25 20:23

免费评分

参与人数 24吾爱币 +25 热心值 +21 收起 理由
yzy93 + 1 + 1 谢谢@Thanks!
LJ0815923 + 1 我很赞同!
ninthcrack + 1 + 1 谢谢@Thanks!
bjznhxy + 1 + 1 谢谢@Thanks!
longshenger + 1 + 1 谢谢@Thanks!
lcldh + 1 + 1 依旧感谢
live2 + 1 谢谢@Thanks!
百事可乐丶 + 1 + 1 老哥,PDF 识别更名 那个能重新上传一下么
armiyi + 1 我很赞同!
LRainner + 1 + 1 用心讨论,共获提升!
chao8709 + 1 + 1 热心回复!
chmwyy + 1 + 1 谢谢@Thanks!
Capybara + 1 谢谢@Thanks!
15235109295 + 1 + 1 我很赞同!
liudejun + 1 + 1 我很赞同!
myq12366 + 1 + 1 用心讨论,共获提升!
风之暇想 + 7 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
yanglinman + 1 谢谢@Thanks!
lhr0802 + 1 + 1 谢谢@Thanks!
李佑辰 + 1 懒揍云:https://l13144.lanzoul.com/iYRtl00megud
wwwxxxddd999 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
26MountainGhos + 1 + 1 我很赞同!
tianbukongbai + 1 + 1 谢谢@Thanks!
andytang866 + 1 热心回复!

查看全部评分

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

 楼主| d8349565 发表于 2022-2-26 12:20
唐古拉 发表于 2022-2-26 12:15
有批量修改的吗?

目录下所有txt文件都会被修改替换
 楼主| d8349565 发表于 2022-2-25 13:13
源码分享
有兴趣的可以交流

[Python] 纯文本查看 复制代码
# -*- coding:utf-8 -*-
import wx
import os

class Frame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title='TXT文本处理软件', size=(467, 426),name='frame',style=541072384)
        self.qdck = wx.Panel(self)
        self.SetTransparent(230)
        self.qdck.SetOwnBackgroundColour((119, 202, 160, 255))
        self.Centre()
        self.bjk1 = wx.TextCtrl(self.qdck,size=(180, 40),pos=(30, 40),value='',name='text',style=0)
        self.bjk2 = wx.TextCtrl(self.qdck,size=(180, 40),pos=(230, 40),value='',name='text',style=0)
        self.bq1 = wx.StaticText(self.qdck,size=(180, 20),pos=(30, 10),label='被替换内容',name='staticText',style=2321)
        self.bq2 = wx.StaticText(self.qdck,size=(180, 20),pos=(230, 10),label='替换内容',name='staticText',style=2321)
        bq1_font = wx.Font(12, 74, 90, 400, False, 'Microsoft YaHei UI', 28)
        self.bq1.SetFont(bq1_font)
        self.bq2.SetFont(bq1_font)
        self.bjk3 = wx.TextCtrl(self.qdck,size=(180, 40),pos=(30, 90),value='',name='text',style=0)
        self.bjk4 = wx.TextCtrl(self.qdck,size=(180, 40),pos=(230, 90),value='',name='text',style=0)
        self.bjk5 = wx.TextCtrl(self.qdck,size=(180, 40),pos=(30, 140),value='',name='text',style=0)
        self.bjk6 = wx.TextCtrl(self.qdck,size=(180, 40),pos=(230, 140),value='',name='text',style=0)
        self.bjk7 = wx.TextCtrl(self.qdck,size=(180, 40),pos=(30, 190),value='',name='text',style=0)
        self.bjk8 = wx.TextCtrl(self.qdck,size=(180, 40),pos=(230, 190),value='',name='text',style=0)
        self.an1 = wx.Button(self.qdck,size=(377, 47),pos=(30, 280),label='执行替换',name='button')
        self.an1.Bind(wx.EVT_BUTTON,self.an1_anbdj)
        self.dxk1 = wx.RadioButton(self.qdck,size=(180, 39),pos=(30, 240),name='radioButton',label='修改文件名和TXT内容')
        self.dxk2 = wx.RadioButton(self.qdck,size=(180, 39),pos=(230, 240),name='radioButton',label='仅修改TXT内容')
        self.dxk2.SetValue(True)



    def an1_anbdj(self,event):
        dir_path = wx.DirSelector(message='选择需要处理的文件目录', style=0, pos=(57, 322), parent=self.qdck)
        file_list = list(os.scandir(dir_path))
        print(file_list)
        text01 = self.bjk1.GetValue()
        text02 = self.bjk3.GetValue()
        text03 = self.bjk5.GetValue()
        text04 = self.bjk7.GetValue()
        retext01 = self.bjk2.GetValue()
        retext02 = self.bjk4.GetValue()
        retext03 = self.bjk6.GetValue()
        retext04 = self.bjk8.GetValue()
        text_list = [text01, text02, text03, text04]
        retext_list = [retext01, retext02, retext03, retext04]
        dict01 = dict(zip(text_list,retext_list))
        for file in list(file_list):
            if '.txt' in os.path.basename(file):
                file_path = os.path.abspath(file)
                file_name_basic = os.path.basename(file)
                file_name = file_name_basic
                print(file_name)
                if self.dxk1.GetValue() == True:
                    print('-----------修改文件名和TXT内容')
                    for x,y in dict01.items():
                        print(x, y)
                        try:
                            file_name = file_name.replace(x, y)
                            print(file_name)
                        except:
                            print(x,y,'修改名称失败')
                    print(file_name)
                    new_file_path = file_path.replace(file_name_basic, f'[已替换]{file_name}')
                    print(new_file_path)
                    with open(file, 'r', encoding='utf-8') as f, open(new_file_path, 'w', encoding='utf-8') as w:
                        text = f.read()
                        for x, y in dict01.items():
                            print(x, y)
                            text = text.replace(x, y)
                        w.write(text)
                else:
                    print('-----------修改TXT内容')
                    new_file_path = file_path.replace(file_name, f'[已替换]{file_name}')
                    with open(file, 'r', encoding='utf-8') as f, open(new_file_path, 'w', encoding='utf-8') as w:
                        text = f.read()
                        for x, y in dict01.items():
                            text = text.replace(x, y)
            else:
                continue
        os.startfile(dir_path)
        print('an1,按钮被单击')



class myApp(wx.App):
    def  OnInit(self):
        self.frame = Frame()
        self.frame.Show(True)
        return True

if __name__ == '__main__':
    app = myApp()
    app.MainLoop()

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
Azad + 1 + 1 谢谢@Thanks!

查看全部评分

xiaomm250 发表于 2022-2-25 12:58
huangxu 发表于 2022-2-25 12:59
试试怎么样,谢谢分享
godsoncai 发表于 2022-2-25 13:00
谢谢楼主,顶上!
stevenchenxf 发表于 2022-2-25 13:10
能搞个批量excel和word文本替换就好了

免费评分

参与人数 1吾爱币 +2 收起 理由
d8349565 + 2 也不是不可以,就是耗时间,可以考虑以后往这方面优化

查看全部评分

swhyy 发表于 2022-2-25 13:18
谢谢楼主分享
qianyi9 发表于 2022-2-25 14:20
感谢楼主,试试看怎么样
benmagic 发表于 2022-2-25 15:04
谢谢楼主分享。
Gordon_c 发表于 2022-2-25 16:22
python 怎么变化exe ??
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 提醒:禁止复制他人回复等『恶意灌水』行为,违者重罚!

快速回复 收藏帖子 返回列表 搜索

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

GMT+8, 2024-3-29 22:44

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

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