1 使用背景
理正的.spw文件是文本格式,类似于该软件的前处理,相关参数字段可通过文本替换,快速修改参数。
后续用途可用在:用EXCEL整理数据,通过修改文本批量获取多个截面参数的spw文件
2 ExcelSheet-shift-textstring
2.1 Code
import os
import re
import chardet
import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
from openpyxl import load_workbook
def select_excel_file():
excel_path = filedialog.askopenfilename(filetypes=[("Excel files", "*.xlsx")])
entry_excel.delete(0, tk.END)
entry_excel.insert(0, excel_path)
def select_text_file():
text_path = filedialog.askopenfilename(filetypes=[("Text files", "*.txt")])
entry_text.delete(0, tk.END)
entry_text.insert(0, text_path)
def replace_string():
excel_path = entry_excel.get()
text_path = entry_text.get()
cell = entry_cell.get()
target_string = entry_target.get()
if not (os.path.exists(excel_path) and os.path.exists(text_path) and cell.strip() and target_string.strip()):
messagebox.showerror("Error", "Please select both files and enter the cell and target string.")
return
wb = load_workbook(excel_path)
ws = wb.active
replacement_string = ws[cell].value
with open(text_path, "rb") as file:
raw_data = file.read()
detected_encoding = chardet.detect(raw_data)
encoding = detected_encoding["encoding"]
with open(text_path, "r", encoding=encoding) as file:
content = file.read()
content = re.sub(target_string, str(replacement_string), content)
with open(text_path, "w", encoding="utf-8") as file:
file.write(content)
messagebox.showinfo("Success", "String replaced successfully.")
root = tk.Tk()
root.title("String Replacer")
frame = tk.Frame(root)
frame.pack(padx=10, pady=10)
label_excel = tk.Label(frame, text="Excel file:")
label_excel.grid(row=0, column=0, sticky="e")
entry_excel = tk.Entry(frame, width=40)
entry_excel.grid(row=0, column=1)
button_excel = tk.Button(frame, text="Browse", command=select_excel_file)
button_excel.grid(row=0, column=2)
label_text = tk.Label(frame, text="Text file:")
label_text.grid(row=1, column=0, sticky="e")
entry_text = tk.Entry(frame, width=40)
entry_text.grid(row=1, column=1)
button_text = tk.Button(frame, text="Browse", command=select_text_file)
button_text.grid(row=1, column=2)
label_cell = tk.Label(frame, text="Cell:")
label_cell.grid(row=2, column=0, sticky="e")
entry_cell = tk.Entry(frame, width=10)
entry_cell.grid(row=2, column=1, sticky="w")
label_target = tk.Label(frame, text="Target string:")
label_target.grid(row=3, column=0, sticky="e")
entry_target = tk.Entry(frame, width=40)
entry_target.grid(row=3, column=1)
button_replace = tk.Button(frame, text="Replace String", command=replace_string)
button_replace.grid(row=4, column=1, pady=10)
root.mainloop()
2.2 界面
2.3 方法解析
- 导入所需的库和模块:
import os
import re
import chardet
import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
from openpyxl import load_workbook
- 定义函数
select_excel_file()
,用于打开文件对话框,选择 Excel 文件,并将其路径显示在 Entry 控件中:
def select_excel_file():
excel_path = filedialog.askopenfilename(filetypes=[("Excel files", "*.xlsx")])
entry_excel.delete(0, tk.END)
entry_excel.insert(0, excel_path)
- 定义函数
select_text_file()
,用于打开文件对话框,选择文本文件,并将其路径显示在 Entry 控件中:
def select_text_file():
text_path = filedialog.askopenfilename(filetypes=[("Text files", "*.txt")])
entry_text.delete(0, tk.END)
entry_text.insert(0, text_path)
- 定义函数
replace_string()
,用于从 Excel 文件中的指定单元格读取数据,并在文本文件中替换目标字符串:
def replace_string():
# 获取用户输入的文件路径、单元格和目标字符串
excel_path = entry_excel.get()
text_path = entry_text.get()
cell = entry_cell.get()
target_string = entry_target.get()
# 检查输入是否有效
if not (os.path.exists(excel_path) and os.path.exists(text_path) and cell.strip() and target_string.strip()):
messagebox.showerror("Error", "Please select both files and enter the cell and target string.")
return
# 读取 Excel 文件中的替换字符串
wb = load_workbook(excel_path)
ws = wb.active
replacement_string = ws[cell].value
# 使用 chardet 检测文本文件的编码,并读取文件内容
with open(text_path, "rb") as file:
raw_data = file.read()
detected_encoding = chardet.detect(raw_data)
encoding = detected_encoding["encoding"]
with open(text_path, "r", encoding=encoding) as file:
content = file.read()
# 替换目标字符串
content = re.sub(target_string, str(replacement_string), content)
# 将替换后的内容写回文本文件
with open(text_path, "w", encoding="utf-8") as file:
file.write(content)
# 显示操作成功的消息框
messagebox.showinfo("Success", "String replaced successfully.")
- 设置 Tkinter GUI 应用程序的基本结构:
root = tk.Tk()
root.title("String Replacer")
frame = tk.Frame(root)
frame.pack(padx=10, pady=10)
# 3 创建用于选择文件和输入单元格、目标字符串的标签、输入框和按钮
# 4 对应的 grid 函数设置它们在界面中的位置
# 5 ...
- 创建 “Replace String” 按钮,绑定
replace_string()
函数,并设置按钮在界面中的位置:
button_replace = tk.Button(frame, text="Replace String", command=replace_string)
button_replace.grid(row=4, column=1, pady=10)
- 启动 Tkinter GUI 应用程序的主循环:
root.mainloop()
整个程序的功能是通过一个简单的 GUI 界面,让用户选择一个 Excel 文件和一个文本文件,输入一个单元格引用和一个目标字符串。然后,程序从 Excel 文件中读取单元格中的数据,将文本文件中的目标字符串替换为该数据,并将结果保存回文本文件。