-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
129 lines (106 loc) · 5.25 KB
/
main.py
File metadata and controls
129 lines (106 loc) · 5.25 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
"""
Excel Split Tool
A utility for splitting Excel workbooks by department or organizational unit.
Processes account data across multiple sheets and creates separate files for each department.
"""
import pandas as pd
import os
def split_workbook_by_department(input_file, output_folder):
"""
按部门拆分Excel工作簿
Args:
input_file (str): 输入Excel文件路径
output_folder (str): 输出文件夹路径
"""
# 创建输出文件夹
if not os.path.exists(output_folder):
os.makedirs(output_folder)
# 读取所有工作表
formal_df = pd.read_excel(input_file, sheet_name='正式账号')
temp_df = pd.read_excel(input_file, sheet_name='临时账号')
public_df = pd.read_excel(input_file, sheet_name='公用账号')
public_users_df = pd.read_excel(input_file, sheet_name='公用账号使用人')
# 打印列名以便调试
print("正式账号表的列名:", formal_df.columns.tolist())
print("临时账号表的列名:", temp_df.columns.tolist())
print("公用账号表的列名:", public_df.columns.tolist())
# 查找包含目标文字的列名
def find_column(df, text):
for col in df.columns:
if isinstance(col, str) and text in col.replace('\n', ''):
return col
return None
# 获取实际的列名
formal_dept_col = find_column(formal_df, '门户组织机构第二层级')
temp_dept_col = find_column(temp_df, '门户组织机构第二层级')
public_dept_col = find_column(public_df, '公用账号所属单位(二)')
if not all([formal_dept_col, temp_dept_col, public_dept_col]):
missing_cols = []
if not formal_dept_col:
missing_cols.append("正式账号表中的'门户组织机构第二层级'")
if not temp_dept_col:
missing_cols.append("临时账号表中的'门户组织机构第二层级'")
if not public_dept_col:
missing_cols.append("公用账号表中的'公用账号所属单位(二)'")
raise ValueError(f"未找到以下列:{', '.join(missing_cols)}")
# 获取所有部门的集合
departments = set()
departments.update(formal_df[formal_dept_col].unique())
departments.update(temp_df[temp_dept_col].unique())
departments.update(public_df[public_dept_col].unique())
# 移除任何空值
departments.discard(None)
departments.discard('')
departments.discard(pd.NA)
print(f"找到的部门列表:{departments}")
# 为每个部门创建新的工作簿
for dept in departments:
# 创建ExcelWriter对象
output_file = os.path.join(output_folder, f'{dept}账号清单.xlsx')
with pd.ExcelWriter(output_file, engine='openpyxl') as writer:
# 筛选并写入正式账号
dept_formal = formal_df[formal_df[formal_dept_col] == dept]
if not dept_formal.empty:
dept_formal.to_excel(writer, sheet_name='正式账号', index=False)
# 筛选并写入临时账号
dept_temp = temp_df[temp_df[temp_dept_col] == dept]
if not dept_temp.empty:
dept_temp.to_excel(writer, sheet_name='临时账号', index=False)
# 筛选并写入公用账号
dept_public = public_df[public_df[public_dept_col] == dept]
if not dept_public.empty:
dept_public.to_excel(writer, sheet_name='公用账号', index=False)
# 获取该部门的公用账号列表
public_account_col = find_column(public_df, '账号')
if not public_account_col:
raise ValueError("在公用账号表中未找到'账号'列")
dept_public_accounts = dept_public[public_account_col].tolist()
# 筛选相关的公用账号使用人
public_users_account_col = find_column(public_users_df, '公用账号')
if not public_users_account_col:
raise ValueError("在公用账号使用人表中未找到'公用账号'列")
dept_public_users = public_users_df[
public_users_df[public_users_account_col].isin(dept_public_accounts)
]
# 如果有相关的使用人记录,写入工作表
if not dept_public_users.empty:
dept_public_users.to_excel(writer, sheet_name='公用账号使用人', index=False)
if __name__ == '__main__':
import sys
# 配置部分 - 用户可以修改这些值
input_file = 'portal-account-audit-form.xlsx'
output_folder = '按部门拆分'
try:
print(f"开始处理文件: {input_file}")
split_workbook_by_department(input_file, output_folder)
print(f"✅ 成功完成!输出文件保存在: {output_folder}")
except FileNotFoundError:
print(f"❌ 错误:找不到输入文件 '{input_file}'")
print("请确认文件路径是否正确")
sys.exit(1)
except ValueError as e:
print(f"❌ 数据错误:{e}")
sys.exit(1)
except Exception as e:
print(f"❌ 意外错误:{e}")
sys.exit(1)