news 2026/4/16 19:10:35

python语言TXT文件批量分割工具软件代码QZQ

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
python语言TXT文件批量分割工具软件代码QZQ
importtkinterastkfromtkinterimportfiledialog,messagebox,ttkimportosclassTxtBatchSplitter:def__init__(self,root):self.root=root self.root.title("TXT文件批量分割工具")self.root.geometry("650x600")# 设置窗口大小# 核心变量self.source_file_path=tk.StringVar()# 源TXT文件路径self.output_dir=tk.StringVar()# 输出目录路径self.split_count=tk.IntVar(value=15)# 预设分割数量为15self.title_prefix=tk.StringVar(value="")# 标题前缀# 创建界面控件self.create_widgets()defcreate_widgets(self):# 1. 源文件选择区域frame1=ttk.LabelFrame(self.root,text="源文件设置",padding=10)frame1.pack(fill="x",padx=20,pady=10)ttk.Label(frame1,text="源TXT文件:").grid(row=0,column=0,sticky="w",padx=5,pady=5)ttk.Entry(frame1,textvariable=self.source_file_path,width=50,state="readonly").grid(row=0,column=1,padx=5,pady=5)ttk.Button(frame1,text="选择文件",command=self.select_source_file).grid(row=0,column=2,padx=5,pady=5)# 2. 分割参数设置区域frame2=ttk.LabelFrame(self.root,text="分割参数设置",padding=10)frame2.pack(fill="x",padx=20,pady=10)# 分割数量ttk.Label(frame2,text="分割数量:").grid(row=0,column=0,sticky="w",padx=5,pady=5)spin_box=ttk.Spinbox(frame2,from_=1,to=100,textvariable=self.split_count,width=10)spin_box.grid(row=0,column=1,padx=5,pady=5)ttk.Label(frame2,text="(默认预设15个文件)").grid(row=0,column=2,sticky="w",padx=5,pady=5)# 标题前缀ttk.Label(frame2,text="文件标题前缀:").grid(row=1,column=0,sticky="w",padx=5,pady=5)ttk.Entry(frame2,textvariable=self.title_prefix,width=20).grid(row=1,column=1,padx=5,pady=5)# 3. 输出目录选择区域frame3=ttk.LabelFrame(self.root,text="输出设置",padding=10)frame3.pack(fill="x",padx=20,pady=10)ttk.Label(frame3,text="输出目录:").grid(row=0,column=0,sticky="w",padx=5,pady=5)ttk.Entry(frame3,textvariable=self.output_dir,width=50,state="readonly").grid(row=0,column=1,padx=5,pady=5)ttk.Button(frame3,text="选择目录",command=self.select_output_dir).grid(row=0,column=2,padx=5,pady=5)# 4. 执行按钮区域frame4=ttk.Frame(self.root,padding=10)frame4.pack(fill="x",padx=20,pady=20)self.split_btn=ttk.Button(frame4,text="开始批量分割",command=self.start_split,width=20)# 修正后的居中方式self.split_btn.pack(padx=10,pady=5)# 5. 进度提示标签self.progress_label=ttk.Label(self.root,text="状态:等待执行",foreground="gray")self.progress_label.pack(pady=10)defselect_source_file(self):"""选择源TXT文件"""file_path=filedialog.askopenfilename(title="选择要分割的TXT文件",filetypes=[("文本文档","*.txt"),("所有文件","*.*")])iffile_path:self.source_file_path.set(file_path)self.progress_label.config(text=f"状态:已选择源文件:{os.path.basename(file_path)}",foreground="blue")defselect_output_dir(self):"""选择输出目录"""dir_path=filedialog.askdirectory(title="选择输出目录")ifdir_path:self.output_dir.set(dir_path)self.progress_label.config(text=f"状态:已选择输出目录:{dir_path}",foreground="blue")defread_source_file(self):"""读取源文件内容"""try:withopen(self.source_file_path.get(),"r",encoding="utf-8")asf:content=f.read()returncontentexceptExceptionase:messagebox.showerror("错误",f"读取源文件失败:{str(e)}")returnNonedefsplit_content(self,content):"""按分割数量均分内容(若无法均分,最后一个文件包含剩余内容)"""split_num=self.split_count.get()content_length=len(content)# 计算每个文件的基础长度base_length=content_length//split_num split_contents=[]foriinrange(split_num):start_idx=i*base_length# 最后一个文件取到末尾ifi==split_num-1:end_idx=content_lengthelse:end_idx=(i+1)*base_length sub_content=content[start_idx:end_idx]split_contents.append(sub_content)returnsplit_contentsdefgenerate_split_files(self,split_contents):"""生成分割后的TXT文件(包含对应标题、Java代码块标记和内容)"""output_dir=self.output_dir.get()title_prefix=self.title_prefix.get()split_num=self.split_count.get()source_filename=os.path.splitext(os.path.basename(self.source_file_path.get()))[0]try:foridx,sub_contentinenumerate(split_contents,1):# 构造每个分割文件的标题和文件名file_title=f"{title_prefix}{idx}(总第{idx}/{split_num}个)"# 构造输出文件名output_filename=f"{source_filename}{idx}.txt"output_filepath=os.path.join(output_dir,output_filename)# 写入文件(标题 + Java代码块开始 + 内容 + Java代码块结束 + 空行)withopen(output_filepath,"w",encoding="utf-8")asf:# 写入标题f.write(f"======{file_title}======\n")# 标题后添加```java\n(Java代码块开始标记)f.write("```java\n")f.write("\n")# 空行分隔标题标记和内容# 写入分割后的子内容f.write(sub_content)# 内容末尾添加```\n\n\n(Java代码块结束标记 + 3个换行符)f.write("\n```\n\n\n")self.progress_label.config(text=f"状态:正在生成第{idx}/{split_num}个文件...",foreground="orange")self.root.update_idletasks()# 刷新界面messagebox.showinfo("成功",f"已成功将文件分割为{split_num}个TXT文件!\n输出目录:{output_dir}")self.progress_label.config(text=f"状态:分割完成!共生成{split_num}个文件",foreground="green")exceptExceptionase:messagebox.showerror("错误",f"生成文件失败:{str(e)}")self.progress_label.config(text="状态:分割失败",foreground="red")defstart_split(self):"""执行分割主流程"""# 校验参数ifnotself.source_file_path.get():messagebox.warning("提示","请先选择要分割的源TXT文件!")returnifnotself.output_dir.get():messagebox.warning("提示","请先选择输出目录!")returnifself.split_count.get()<1:messagebox.warning("提示","分割数量不能小于1!")return# 读取源文件self.progress_label.config(text="状态:正在读取源文件...",foreground="orange")self.root.update_idletasks()content=self.read_source_file()ifnotcontent:return# 分割内容self.progress_label.config(text="状态:正在分割内容...",foreground="orange")self.root.update_idletasks()split_contents=self.split_content(content)# 生成文件self.progress_label.config(text="状态:正在生成分割文件...",foreground="orange")self.root.update_idletasks()self.generate_split_files(split_contents)if__name__=="__main__":root=tk.Tk()app=TxtBatchSplitter(root)root.mainloop()
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/16 9:13:17

深入理解AUTOSAR NM报文唤醒的集成策略

AUTOSAR NM报文唤醒&#xff1a;从机制到实战的深度拆解在一辆现代智能汽车中&#xff0c;当你轻拉车门把手的瞬间&#xff0c;车内氛围灯渐次亮起、仪表盘启动迎宾动画、空调系统悄然恢复运行——这些看似简单的联动背后&#xff0c;其实是一场精密的“电子交响乐”。而指挥这…

作者头像 李华
网站建设 2026/4/16 9:13:16

Gradio多模态集成避坑指南(90%新手都会犯的4个错误)

第一章&#xff1a;Gradio多模态模型Demo概述Gradio 是一个轻量级的 Python 库&#xff0c;专为快速构建机器学习和深度学习模型的交互式 Web 界面而设计。它支持多种输入输出类型&#xff0c;包括文本、图像、音频、视频以及组合形式&#xff0c;非常适合用于多模态模型的演示…

作者头像 李华
网站建设 2026/4/16 14:31:49

PCB电镀+蚀刻工艺优化:全面讲解提升良率的关键步骤

PCB电镀与蚀刻协同优化&#xff1a;从工艺缺陷到良率跃升的实战指南 你有没有遇到过这样的情况&#xff1f; 明明设计没问题&#xff0c;光绘数据也核对无误&#xff0c;可做出来的板子就是频频出现“短路”、“断线”&#xff0c;AOI报一堆桥接和缺口。返工几轮后才发现——问…

作者头像 李华
网站建设 2026/4/16 7:07:22

elasticsearch-head多集群管理:高效运维操作指南

用 elasticsearch-head 玩转多集群运维&#xff1a;一个轻量但高效的实战指南 你有没有遇到过这样的场景&#xff1f; 手头管着开发、测试、预发、生产好几套 Elasticsearch 集群&#xff0c;每次查健康状态都得翻终端记录&#xff1b;想看一眼某个索引的分片分布&#xff0c…

作者头像 李华
网站建设 2026/4/16 12:45:46

ComfyUI-SeedVR2视频超分辨率完整指南:让模糊视频重获新生

ComfyUI-SeedVR2视频超分辨率完整指南&#xff1a;让模糊视频重获新生 【免费下载链接】ComfyUI-SeedVR2_VideoUpscaler Non-Official SeedVR2 Vudeo Upscaler for ComfyUI 项目地址: https://gitcode.com/gh_mirrors/co/ComfyUI-SeedVR2_VideoUpscaler 还在为老旧的视频…

作者头像 李华
网站建设 2026/4/16 7:15:34

Git commit规范检测工具链整合VoxCPM-1.5-TTS-WEB-UI语音反馈

Git commit规范检测工具链整合VoxCPM-1.5-TTS-WEB-UI语音反馈 在现代软件开发中&#xff0c;代码协作的规范化与自动化正变得越来越重要。一个团队每天可能产生数十甚至上百次提交&#xff0c;而确保每一次 git commit 都符合约定格式——比如使用 Angular 风格的 type(scope):…

作者头像 李华