我用python写的一个小说格式化工具(去除中英文标点符号)
2025-03-23 17:25:28 225 分享链接 开发笔记 python
import string
import tkinter as tk
from tkinter import scrolledtext
from tkinter import messagebox
# 合并中文和英文标点符号
punctuations = string.punctuation + '!?。。,、;:“”‘’()〔〕【】﹃﹄「」﹁﹂—…-~《》〈〉'
def remove_punctuation_and_newline(text):
result = ""
for char in text:
if char in punctuations:
result += '\n'
else:
result += char
# 去除多余的空行
result = '\n'.join(line.strip() for line in result.split('\n') if line.strip())
return result
def convert_text():
input_text = input_textbox.get("1.0", tk.END).strip()
output_text = remove_punctuation_and_newline(input_text)
output_textbox.delete("1.0", tk.END)
output_textbox.insert(tk.END, output_text)
def clear_input_text():
input_textbox.delete("1.0", tk.END)
def copy_output_text():
output_text = output_textbox.get("1.0", tk.END).strip()
if not output_text:
messagebox.showerror("错误", "输出框没有内容,无法复制!")
return
try:
root.clipboard_clear()
root.clipboard_append(output_text)
messagebox.showinfo("提示", "复制成功!")
except Exception as e:
messagebox.showerror("错误", f"复制失败,原因:{str(e)}")
# 创建主窗口
root = tk.Tk()
root.title("标点符号去除并换行工具")
# 设置窗口大小
root.geometry("1024x768")
# 配置网格布局的列权重
root.columnconfigure(0, weight=45)
root.columnconfigure(1, weight=10)
root.columnconfigure(2, weight=45)
root.rowconfigure(0, weight=1)
# 创建输入文本框
input_textbox = scrolledtext.ScrolledText(root, width=60, height=40)
input_textbox.grid(row=0, column=0, padx=10, pady=10, sticky="nsew")
# 创建按钮框架
button_frame = tk.Frame(root)
button_frame.grid(row=0, column=1, padx=10, pady=10, sticky="n")
# 创建转换按钮
convert_button = tk.Button(button_frame, text="点击转换", command=convert_text)
convert_button.pack(pady=10)
# 创建清除文本按钮
clear_button = tk.Button(button_frame, text="清除文本", command=clear_input_text)
clear_button.pack(pady=10)
# 创建复制结果按钮
copy_button = tk.Button(button_frame, text="复制结果", command=copy_output_text)
copy_button.pack(pady=10)
# 创建输出文本框
output_textbox = scrolledtext.ScrolledText(root, width=60, height=40)
output_textbox.grid(row=0, column=2, padx=10, pady=10, sticky="nsew")
# 运行主循环
root.mainloop()
最近更新
- 2025-08-04 16:13
- 详细介绍一下 tkinter 的pack布局参数
- 2025-08-03 17:50
- pyinstaller --onefile --windowed 与 pyinstaller -F -w的区别
- 2025-08-03 17:39
- 使用 PyInstaller 打包 Python 程序时 隐藏调用其它程序的命令窗口。
- 2025-08-03 11:04
- 使用 PyInstaller 打包 Python 程序时 -F 与 -D的区别。
- 2025-08-01 15:15
- 通过Edge-tts生成的中文字幕如何自然断句?
- 2025-07-31 18:23
- Edge-tts库 命令行工具有哪些常用参数?
- 2025-07-29 01:43
- 豆包连环画生成提示(优化版)
- 2025-07-28 13:20
- 镜头运动手法:不止推拉,这些技巧让画面更有张力。
- 2025-07-28 13:13
- 摄影与剪辑是视频创作的两个核心环节,二者共同决定了作品的最终呈现效果。
- 2025-07-23 16:35
- 在Python中如何获取脚本所在的目录?