-
Notifications
You must be signed in to change notification settings - Fork 1
/
export.py
58 lines (50 loc) · 1.63 KB
/
export.py
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
from bson import ObjectId
from database import connect_to_mongo, db
import pandas as pd
from tqdm import tqdm
async def main():
await connect_to_mongo()
users = await db.zvms.users.find({}).to_list(None)
data = []
# data = {
# '_id': [],
# 'ID': [],
# 'Name': [],
# 'Class ID': [],
# 'On Campus': [],
# 'Off Campus': [],
# 'Social Practice': []
# }
for user in tqdm(users):
class_id = await db.zvms.groups.find_one(
{
"_id": {"$in": [ObjectId(group) for group in user["group"]]},
"type": "class"
}
)
if class_id is None:
class_id = None
else:
class_id = class_id["name"]
data.append({
'_id': str(user["_id"]),
'ID': user["id"],
'Name': user["name"],
'Class ID': class_id,
'On Campus': 0,
'Off Campus': 0,
'Social Practice': 0,
'Grade': class_id[1] if class_id is not None else ''
})
df = pd.DataFrame(data)
df.sort_values("ID", ascending=True)
grouped = df.groupby('Grade', as_index=False)
with pd.ExcelWriter("namelist.xlsx", engine="xlsxwriter") as writer:
# Iterate over each group and write to a separate sheet
for grade, group_df in grouped:
group_df = group_df.sort_values(by=['Class ID', 'ID'], ascending=True)
group_df = group_df.drop('Grade', axis=1)
group_df.to_excel(writer, sheet_name=str(grade), index=False)
if __name__ == "__main__":
import asyncio
asyncio.run(main())