目录
  1. 1. 窗口效果
  2. 2. 输出结果
  3. 3. 源码
python实现族库统计到excel表

在使用python调用的Tkinter窗口选择文件夹,对该文件及子文件夹中的文件进行统计,将统计结果输出到Excel表中,表中将包含文件完整路径,文件格式,文件各级文件夹名称,同时对二级文件夹中的文件夹分类统计,并输出柱状图用于展示;

窗口效果

窗口

输出结果

结果

源码

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()

# 创建Excel工作簿和表格
workbook = Workbook()
sheet = workbook.active

# 设置Excel表头
sheet.append(["文件完整路径", "文件格式", "一级文件夹", "二级文件夹", "三级文件夹", "四级文件夹", "五级文件夹"])

# 统计文件并填入Excel
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

# 提取列 C 的数据(排除空值)
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]

# 创建表格1:列 C 唯一值及个数
table1_data = {'唯一值': column_C_unique, '个数': [column_C.count(val) for val in column_C_unique]}
table1_df = pd.DataFrame(table1_data)

# 将数据写入Excel表中
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的个数用于限制后续数据范围
table1_df_count = len(table1_df)
print(table1_df_count)

# 获取数据范围 其中min_col=11为列数,min_row=1为开始行数
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 = "数量"

# 将图表插入到Excel表中
sheet.add_chart(chart, "M2")

# 调整图表大小
chart.width = 15
chart.height = 10

# 保存Excel文件
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" # 将链接地址设置为百度网站 URL

# 创建链接标签对象,并将其添加到窗口中
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)

# 是否只统计.rfa文件的复选框
include_rfa_var = tk.BooleanVar(value=True) # 默认只统计.rfa文件
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()

文章作者: 嗜血星空earth
文章链接: http://sxxkearth.github.io/2023/06/17/python%E5%AE%9E%E7%8E%B0%E6%97%8F%E5%BA%93%E7%BB%9F%E8%AE%A1%E5%88%B0excel%E8%A1%A8/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请附以署名及出处!

评论