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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
| import os import tkinter as tk from tkinter import filedialog, messagebox from openpyxl import Workbook from openpyxl.styles import PatternFill, Font import pandas as pd from openpyxl.chart import BarChart, Reference import webbrowser
def count_files(folder_path, sheet, include_rfa): for root, dirs, files in os.walk(folder_path): for file in files: file_path = os.path.join(root, file) file_name, file_extension = os.path.splitext(file)
if include_rfa and file_extension.lower() != '.rfa': continue elif not include_rfa and file_extension.lower() == '.xxx': continue
folder_names = root.split(os.sep) folder_names = folder_names[1:]
sheet.append([file_path, file_extension] + folder_names)
def open_file_dialog(): global folder_path_entry
folder_path = filedialog.askdirectory(title='选择文件夹') folder_path_entry.delete(0, tk.END) folder_path_entry.insert(0, folder_path)
def start_statistics(): folder_path = folder_path_entry.get()
if not os.path.isdir(folder_path): messagebox.showerror("错误", "无效的文件夹路径") return
include_rfa = include_rfa_var.get()
workbook = Workbook() sheet = workbook.active
sheet.append(["文件完整路径", "文件格式", "一级文件夹", "二级文件夹", "三级文件夹", "四级文件夹", "五级文件夹"])
count_files(folder_path, sheet, include_rfa)
for column in sheet.columns: max_length = 0 column = [cell for cell in column] for cell in column: try: if len(str(cell.value)) > max_length: max_length = len(cell.value) except: pass adjusted_width = (max_length + 2) * 1.2 sheet.column_dimensions[column[0].column_letter].width = adjusted_width
header_fill = PatternFill(start_color="c00000", end_color="c00000", fill_type="solid") header_font = Font(color="FFFFFF", bold=True)
for cell in sheet[1]: cell.fill = header_fill cell.font = header_font
column_C = [cell.value for cell in sheet['C'][1:] if cell.value is not None] column_C_unique = list(set(column_C)) column_C_count = [column_C.count(val) for val in column_C_unique]
table1_data = {'唯一值': column_C_unique, '个数': [column_C.count(val) for val in column_C_unique]} table1_df = pd.DataFrame(table1_data)
for i, value in enumerate(table1_data['唯一值'], start=1): column = i sheet.cell(row=column, column=10, value=value) sheet.cell(row=column, column=11, value=table1_data['个数'][i - 2])
table1_df_count = len(table1_df) print(table1_df_count)
data = Reference(sheet, min_col=11, min_row=1, max_row=table1_df_count, max_col=11) categories = Reference(sheet, min_col=10, min_row=1, max_row=table1_df_count)
chart = BarChart() chart.add_data(data, titles_from_data=True) chart.set_categories(categories) chart.title = "族库数量图" chart.x_axis_title = "分类" chart.y_axis_title = "数量"
sheet.add_chart(chart, "M2")
chart.width = 15 chart.height = 10
output_path = os.path.join(folder_path, "MIDBIM族库.xlsx") workbook.save(output_path)
messagebox.showinfo("完成", f"文件统计信息已填入Excel表格:{output_path}")
window = tk.Tk() window.title("MIDBIM族库统计") window.geometry("400x300")
menu_bar = tk.Menu(window) window.config(menu=menu_bar)
help_menu = tk.Menu(menu_bar, tearoff=0) menu_bar.add_cascade(label="帮助", menu=help_menu)
def about(): about_window = tk.Toplevel(window) about_window.title("关于") about_window.geometry("300x200")
title_label = tk.Label(about_window, text="MIDBIM族库统计", font=("微软雅黑", 14, "bold"), bg='#b93a2c',fg='white') title_label.pack(pady=10, fill='x') title_label.place(relx=0.5, rely=0.2, anchor='sw') title_label.pack(pady=10)
version_label = tk.Label(about_window, text="版本号:1.0", font=("微软雅黑", 10)) version_label.pack()
author_label = tk.Label(about_window, text="作者:嗜血星空earth", font=("微软雅黑", 10)) author_label.pack()
link = "sxxkearth.github.io"
link_label = tk.Label(about_window, text="sxxkearth.github.io", font=("微软雅黑", 9), fg="blue", cursor="hand2") link_label.pack(pady=10) link_label.bind("<Button-1>", lambda e: webbrowser.open_new(link))
copyright_label = tk.Label(about_window, text="© 2023 嗜血星空earth", font=("微软雅黑", 8)) copyright_label.place(relx=0.5, rely=1, anchor='s')
help_menu.add_command(label="关于", command=about)
explain_label = tk.Label(window, text="说明:本程序用于输出族库文件夹及子文件夹统计表,将对不同级别文件夹进行罗列,输出Excel表格至当前文件夹;", font=("微软雅黑", 10), wraplength=350) explain_label.pack(pady=20)
copyright_label = tk.Label(window, text="© 2023 嗜血星空earth", font=("微软雅黑", 8)) copyright_label.place(relx=0.5, rely=1, anchor='s')
folder_path_label = tk.Label(window, text="输入路径或选择文件夹:",pady=0) folder_path_label.pack()
folder_path_entry = tk.Entry(window, width=30) folder_path_entry.pack()
browse_button = tk.Button(window, text="选择族库文件夹", command=open_file_dialog) browse_button.pack(pady=8)
include_rfa_var = tk.BooleanVar(value=True) include_rfa_checkbutton = tk.Checkbutton(window, text="只统计 .rfa 文件", variable=include_rfa_var,pady=8) include_rfa_checkbutton.pack()
start_button = tk.Button(window, text="开始统计", command=start_statistics) start_button.pack()
window.mainloop()
|