|
|
import os
|
|
|
import json
|
|
|
import openpyxl
|
|
|
|
|
|
# 学生信息字典
|
|
|
student_info = {}
|
|
|
excel_filename = 'student_info.xlsx' # Define the Excel file name
|
|
|
|
|
|
|
|
|
def print_menu():
|
|
|
print('-' * 30)
|
|
|
print('1. 添加学员信息')
|
|
|
print('2. 删除学员信息')
|
|
|
print('3. 修改学员信息')
|
|
|
print('4. 查询学员信息')
|
|
|
print('5. 显示所有学员信息')
|
|
|
print('6. 保存信息')
|
|
|
print('7. 生成Excel表格')
|
|
|
print('8. 退出系统')
|
|
|
print('-' * 30)
|
|
|
|
|
|
|
|
|
def add_student():
|
|
|
# 添加学员信息
|
|
|
name = input('请输入学员姓名:')
|
|
|
student_id = input('请输入学员ID:')
|
|
|
|
|
|
# 判断ID是否存在
|
|
|
if student_id in student_info:
|
|
|
print('该ID已存在,请重新输入!')
|
|
|
return
|
|
|
|
|
|
gender = input('请输入学员性别 (男/女):')
|
|
|
while gender not in ['男', '女']:
|
|
|
print('性别输入有误,请重新输入 (男/女):')
|
|
|
gender = input('请输入学员性别 (男/女):')
|
|
|
|
|
|
phone = input('请输入联系方式:')
|
|
|
student_info[student_id] = {'name': name, 'id': student_id, 'gender': gender, 'phone': phone}
|
|
|
print('学员信息添加成功!')
|
|
|
|
|
|
|
|
|
def delete_student():
|
|
|
# 删除学员信息
|
|
|
student_id = input('请输入要删除的学员ID:')
|
|
|
|
|
|
# 删除时判断ID是否存在
|
|
|
if student_id in student_info:
|
|
|
del student_info[student_id]
|
|
|
print('删除学员信息成功!')
|
|
|
else:
|
|
|
print('该ID不存在,删除失败!')
|
|
|
|
|
|
|
|
|
def modify_student():
|
|
|
# 修改学员信息
|
|
|
student_id = input('请输入要修改的学员ID:')
|
|
|
|
|
|
#
|
|
|
if student_id in student_info:
|
|
|
name = input('请输入新的姓名:')
|
|
|
phone = input('请输入新的联系方式:')
|
|
|
student_info[student_id]['name'] = name
|
|
|
student_info[student_id]['phone'] = phone
|
|
|
print('学员信息修改成功!')
|
|
|
else:
|
|
|
print('该ID不存在,修改失败!')
|
|
|
|
|
|
|
|
|
def search_student():
|
|
|
# 查询学员信息
|
|
|
student_id = input('请输入要查询的学员ID:')
|
|
|
if student_id in student_info:
|
|
|
print(student_info[student_id])
|
|
|
else:
|
|
|
print('该ID不存在!')
|
|
|
|
|
|
|
|
|
def print_all():
|
|
|
# 显示所有学员信息
|
|
|
print('所有学员信息如下:')
|
|
|
for student_id, info in student_info.items():
|
|
|
print(f'ID: {student_id}, 姓名: {info["name"]}, 性别: {info["gender"]}, 联系方式: {info["phone"]}')
|
|
|
|
|
|
|
|
|
def save_info():
|
|
|
# 保存信息到文件和Excel表格
|
|
|
try:
|
|
|
with open('student_info.txt', 'w', encoding='utf-8') as f:
|
|
|
json.dump(student_info, f, ensure_ascii=False, indent=4)
|
|
|
print('信息保存成功!')
|
|
|
|
|
|
# 将信息保存到Excel
|
|
|
save_to_excel()
|
|
|
except Exception as e:
|
|
|
print(f'信息保存失败! Error: {e}')
|
|
|
|
|
|
|
|
|
def save_to_excel():
|
|
|
# 将信息保存到Excel
|
|
|
wb = openpyxl.Workbook()
|
|
|
ws = wb.active
|
|
|
ws.append(['ID', '姓名', '性别', '联系方式'])
|
|
|
|
|
|
for student_id, info in student_info.items():
|
|
|
ws.append([info['id'], info['name'], info['gender'], info['phone']])
|
|
|
|
|
|
wb.save(excel_filename)
|
|
|
print(f'Excel表格保存成功: {excel_filename}')
|
|
|
|
|
|
|
|
|
def load_info():
|
|
|
# 从文件和Excel加载信息
|
|
|
if os.path.exists('student_info.txt'):
|
|
|
try:
|
|
|
with open('student_info.txt', 'r', encoding='utf-8') as f:
|
|
|
data = f.read()
|
|
|
student_info.update(json.loads(data))
|
|
|
print('信息从文件加载成功!')
|
|
|
except Exception as e:
|
|
|
print(f'信息从文件加载失败! Error: {e}')
|
|
|
|
|
|
# 从Excel加载数据
|
|
|
if os.path.exists(excel_filename):
|
|
|
try:
|
|
|
wb = openpyxl.load_workbook(excel_filename)
|
|
|
ws = wb.active
|
|
|
for row in ws.iter_rows(min_row=2, values_only=True):
|
|
|
student_id, name, gender, phone = row
|
|
|
student_info[student_id] = {'name': name, 'id': student_id, 'gender': gender, 'phone': phone}
|
|
|
print('信息从Excel加载成功!')
|
|
|
except Exception as e:
|
|
|
print(f'信息从Excel加载失败! Error: {e}')
|
|
|
|
|
|
|
|
|
def main():
|
|
|
load_info()
|
|
|
|
|
|
while True:
|
|
|
print_menu()
|
|
|
choice = int(input('请选择操作:'))
|
|
|
if choice == 1:
|
|
|
add_student()
|
|
|
elif choice == 2:
|
|
|
delete_student()
|
|
|
elif choice == 3:
|
|
|
modify_student()
|
|
|
elif choice == 4:
|
|
|
search_student()
|
|
|
elif choice == 5:
|
|
|
print_all()
|
|
|
elif choice == 6:
|
|
|
save_info()
|
|
|
elif choice == 7:
|
|
|
save_to_excel() # Save to Excel without saving to file
|
|
|
elif choice == 8:
|
|
|
break
|
|
|
else:
|
|
|
print('输入错误,请重新输入!')
|
|
|
|
|
|
|
|
|
save_info()
|
|
|
|
|
|
|
|
|
print('Current Working Directory:', os.getcwd())
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
main()
|