Repository navigation
Expand file tree
/
Copy pathformat.py
More file actions
147 lines (123 loc) · 5.45 KB
/
Copy pathformat.py
File metadata and controls
147 lines (123 loc) · 5.45 KB
1
2
3
4
5
6
7
8
9
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import tkinter as tk
from tkinter import ttk, Entry
def is_chinese(char):
return "\u4e00" <= char <= "\u9fff"
def is_full_width(char):
return "\uff01" <= char <= "\uff5e"
def duiqi(string, length, way):
if way == "left":
difference = length - len(string)
if difference == 0: # 若差值为0则不需要补
return string
elif difference < 0:
print("错误:限定的对齐长度小于字符串长度!")
return None
new_string = ""
space = " "
for i in string:
codes = ord(i) # 将字符转为ASCII或UNICODE编码
if codes <= 126: # 若是半角字符
new_string = new_string + chr(codes + 65248) # 则转为全角
else:
new_string = new_string + i # 若是全角,则不转换
return new_string + space * (difference) # 返回补齐空格后的字符串
elif way == "right":
difference = length - len(string)
if difference == 0: # 若差值为0则不需要补
return string
elif difference < 0:
print("错误:限定的对齐长度小于字符串长度!")
return None
new_string = ""
space = " "
for i in string:
codes = ord(i) # 将字符转为ASCII或UNICODE编码
if codes <= 126: # 若是半角字符
new_string = new_string + chr(codes + 65248) # 则转为全角
else:
new_string = new_string + i # 若是全角,则不转换
return space * (difference) + new_string # 返回补齐空格后的字符串
elif way == "center":
difference = length - len(string)
if difference == 0: # 若差值为0则不需要补
return string
elif difference < 0:
print("错误:限定的对齐长度小于字符串长度!")
return None
new_string = ""
space = " "
for i in string:
codes = ord(i) # 将字符转为ASCII或UNICODE编码
if codes <= 126: # 若是半角字符
new_string = new_string + chr(codes + 65248) # 则转为全角
else:
new_string = new_string + i # 若是全角,则不转换
return space * (int(difference / 2)) + " " * (difference % 2)+ new_string + space * (int(difference / 2))+ " " * (difference % 2) # 返回补齐空格后的字符串
class TableGenerator:
def __init__(self, alignment="left",ex_str = "#"):
self.rows = []
self.alignment = alignment
self.ex_str= ex_str
self.max_lengths = []
def add_row(self, data):
lengths = [len(item) for item in data]
if not self.max_lengths:
self.max_lengths = [0] * len(lengths)
self.max_lengths = [max(x, y) for x, y in zip(self.max_lengths, lengths)]
self.rows.append(data)
def generate_table(self):
table = ""
for row in self.rows:
formatted_row = "|"
for i, cell in enumerate(row):
# 使用最大长度进行格式化
formatted_row += " " + duiqi(str(cell), self.max_lengths[i] + 1, self.alignment) + " |"
table += self.ex_str +formatted_row + "\n"
return table
def format_text(*args,**kwargs):
ex_str = text_entry.get()
t = TableGenerator(alignment_var.get(),ex_str)
input_text = text_input.get(1.0, tk.END) # 获取输入文本框中的内容
lines = input_text.replace(" ","").replace(ex_str,"").split("\n")
data = [line.split("|") for line in lines]
for d in data:
if d == [""]:
continue
t.add_row(d)
formatted_text = t.generate_table()
text_output.delete(1.0, tk.END) # 清空输出文本框
text_output.insert(1.0, formatted_text) # 将格式化后的文本填入输出文本框
print(text_input.get(1.0, tk.END) )
root = tk.Tk()
root.title("文本格式化器")
root.geometry("800x400") # 设置窗口的默认宽度为600像素,高度为400像素
# 大文本输入框
text_input = tk.Text(root, height=10, width=50)
text_input.grid(row=0, column=0, padx=10, pady=10, sticky="nsew")
# 按钮布局
button_frame = tk.Frame(root)
button_frame.grid(row=1, column=0, padx=10, pady=5, sticky="ew")
# 对齐方式下拉框
alignment_var = tk.StringVar()
alignment_options = ["left", "center","right",]
alignment_dropdown = ttk.Combobox(button_frame, textvariable=alignment_var, values=alignment_options, state="readonly")
alignment_dropdown.current(0) # 设置默认选择
alignment_dropdown.grid(row=0, column=0, padx=5)
# alignment_dropdown.bind("<<ComboboxSelected>>", on_alignment_change)
# 文本输入框
text_entry = Entry(button_frame)
text_entry.insert(0, "#") # 默认文本为#
text_entry.grid(row=0, column=1, padx=5)
# 格式化按钮
format_button = tk.Button(button_frame, text="格式化", command=format_text)
format_button.grid(row=0, column=2, padx=5)
# 文本输出框
text_output = tk.Text(root, height=10, width=50)
text_output.grid(row=2, column=0, padx=10, pady=10, sticky="nsew")
# 实例化TableGenerator
t = TableGenerator()
# 设置行和列的拉伸权重,使文本框可以随着窗口的拉伸而拉伸
root.grid_rowconfigure(0, weight=1)
root.grid_rowconfigure(2, weight=1)
root.grid_columnconfigure(0, weight=1)
root.mainloop()