说明 :经过多次交互,前后花了几个小时,总算完成了基本功能。如果做到按不同层次分配考场,一键出打印结果就完美了。如果不想看中间"艰苦"的过程,请直接跳到"最后结果"及"食用方法"。中间过程还省略了一部分交互,gpt-4o一个线程限制了会话的次数(100),大概开了三个线程完成了任务。在此过程中自己可能提示词描述不够准确。以后在描述问题时还需要改进,后续添加交互,打包为exe
在日常的教务活动中,考场安装很费时的事。学校平时考场安排,不像大型考试每考室人数固定,有时还会按不同层次的学生安排考场,要求比较多,排定后还需手工调整。
第一次给的提示语:
bash
电子表格"考场安排demo.xlsx"的字段为:班级、准考证号、学生姓名、考场、考号。其中考场、考号需要程序来安排的,请按以下要求给出python程序。
1、总考场数为24个
2、请平均分配每考场人数
3、每个考场的座位号从1开始编号,依次为2, 3, 4,...。
4、随机安排学生,注意是随机,但要保证在一个考场中相邻两条记录,它们班级字段的值不相等。
5、将安排好的结果以"考场安排结果.xlsx"保存
经过了几次迭代:
上面的程序执行了很长时间,即没有结果,也没有出错提示
错误仍然存在
如果每考场的学生班号随机一点就更好了
此时出现警告
A value is trying to be set on a copy of a slice from a DataFrame
直接将警告提交给gpt
当我重新运行以上程序时,虽然没有了警告提示,运行很长时间没有结果
这时给出了正确的结果
bash
import pandas as pd
import random
from openpyxl import Workbook
import numpy as np
def load_data(filename):
return pd.read_excel(filename)
def save_data(df, filename):
df.to_excel(filename, index=False)
def assign_seats(df, total_halls):
# 按班级分组并随机打乱每个班级内部的顺序
grouped = df.groupby('班级')
groups = [group.sample(frac=1).reset_index(drop=True) for _, group in grouped]
# 创建调度器队列,初始化为每个班级的iterator
iterators = [iter(group.iterrows()) for group in groups]
arranged_data = []
hall_number = 1
seat_number = 1
total_students = len(df)
students_per_hall = total_students // total_halls
extra_students = total_students % total_halls
while iterators:
random.shuffle(iterators)
for it in iterators[:]: # 遍历每个迭代器
try:
_, student = next(it)
student_data = student.to_dict()
student_data["考场"] = hall_number
student_data["考号"] = seat_number
arranged_data.append(student_data)
seat_number += 1
if seat_number > students_per_hall + (1 if hall_number <= extra_students else 0):
hall_number += 1
seat_number = 1
if hall_number > total_halls:
hall_number = 1
except StopIteration:
iterators.remove(it) # 当前iterator已遍历完全,移除它
return arranged_data
def main():
input_filename = "考场安排demo.xlsx"
output_filename = "考场安排结果.xlsx"
total_halls = 24
df = load_data(input_filename)
arranged_students = assign_seats(df, total_halls)
arranged_df = pd.DataFrame(arranged_students)
save_data(arranged_df, output_filename)
if __name__ == "__main__":
main()
重新给了提示语:座位号改为01、02、03...,添加了时间戳。
电子表格"考场安排demo.xlsx"的字段为:班级、准考证号、学生姓名、考场、考号。其中考场、考号需要程序来安排的,请按以下要求给出python程序。
1、总考场数为24个
2、请平均分配每考场人数
3、每个考场的座位号从01开始编号,依次为02, 03, 04,...。
4、随机安排学生,注意是随机,但要保证在一个考场中相邻两条记录,它们班级字段的值不相等。
5、将安排好的结果以"考场安排结果+时间戳.xlsx"保存
很高兴听到程序已经很好地满足了你的要求。为了确保最终程序完善且清晰,我将提供最终版本的Python代码,该代码完整处理各项要求,并生成考场安排结果文件:
-
总考场数为24个。
-
平均分配每考场人数。
-
每个考场的座位号从01开始编号,依次为02, 03, 04,...。
-
随机安排学生,保证在一个考场中,相邻两条记录的班级字段的值不相等。
-
将安排好的结果以"考场安排结果+时间戳.xlsx"保存。
代码如下:
bash
import pandas as pd
import random
from openpyxl import Workbook
from datetime import datetime
import numpy as np
def load_data(filename):
return pd.read_excel(filename)
def save_data(df, filename):
df.to_excel(filename, index=False)
def assign_seats(df, total_halls):
# 按班级分组并随机打乱每个班级内部的顺序
grouped = df.groupby('班级')
groups = [group.sample(frac=1).reset_index(drop=True) for _, group in grouped]
# 创建调度器队列,初始化为每个班级的iterator
iterators = [iter(group.iterrows()) for group in groups]
arranged_data = []
hall_number = 1
seat_number = 1
total_students = len(df)
students_per_hall = total_students // total_halls
extra_students = total_students % total_halls
while iterators:
random.shuffle(iterators)
for it in iterators[:]:
try:
_, student = next(it)
student_data = student.to_dict()
student_data["考场"] = hall_number
student_data["考号"] = f"{seat_number:02d}" # 格式化为两位字符
arranged_data.append(student_data)
seat_number += 1
if seat_number > students_per_hall + (1 if hall_number <= extra_students else 0):
hall_number += 1
seat_number = 1
if hall_number > total_halls:
hall_number = 1
except StopIteration:
iterators.remove(it)
return arranged_data
def main():
input_filename = "考场安排demo.xlsx"
total_halls = 24
df = load_data(input_filename)
arranged_students = assign_seats(df, total_halls)
arranged_df = pd.DataFrame(arranged_students)
current_time = datetime.now().strftime("%Y%m%d_%H%M%S")
output_filename = f"考场安排结果_{current_time}.xlsx"
save_data(arranged_df, output_filename)
print(f"已成功生成文件:{output_filename}")
if __name__ == "__main__":
main()
让gpt-4o进行了美化:
为了对导出的电子表格进行格式化美化,我们可以使用 pandas
和 openpyxl
模块来提升电子表格的可读性和视觉效果。这可能包括:
-
设置标题行的样式(如背景颜色、字体加粗等)。
-
自动调整列宽。
-
为整个表格设置边框。
-
设置单元格对齐方式。
以下是如何实现这些功能的代码示例:
bash
import pandas as pd
import openpyxl
from openpyxl.styles import Font, Alignment, PatternFill, Border, Side
from openpyxl.utils import get_column_letter
from openpyxl.worksheet.worksheet import Worksheet
import random
from datetime import datetime
def load_data(filename):
return pd.read_excel(filename)
def save_data(df, filename):
df.to_excel(filename, index=False)
format_excel(filename)
def assign_seats(df, total_halls):
grouped = df.groupby('班级')
groups = [group.sample(frac=1).reset_index(drop=True) for _, group in grouped]
iterators = [iter(group.iterrows()) for group in groups]
arranged_data = []
hall_number = 1
seat_number = 1
total_students = len(df)
students_per_hall = total_students // total_halls
extra_students = total_students % total_halls
while iterators:
random.shuffle(iterators)
for it in iterators[:]:
try:
_, student = next(it)
student_data = student.to_dict()
student_data["考场"] = hall_number
student_data["考号"] = f"{seat_number:02d}"
arranged_data.append(student_data)
seat_number += 1
if seat_number > students_per_hall + (1 if hall_number <= extra_students else 0):
hall_number += 1
seat_number = 1
if hall_number > total_halls:
hall_number = 1
except StopIteration:
iterators.remove(it)
return arranged_data
def format_excel(filename):
wb = openpyxl.load_workbook(filename)
ws = wb.active
# 设置标题行的样式
for cell in ws[1]:
cell.font = Font(bold=True, color="FFFFFF")
cell.fill = PatternFill(start_color="4F81BD", end_color="4F81BD", fill_type="solid")
cell.alignment = Alignment(horizontal="center", vertical="center")
# 自动调整列宽
for col in ws.columns:
max_length = 0
column = col[0].column_letter # 获取列的字母
for cell in col:
try:
if len(str(cell.value)) > max_length:
max_length = len(str(cell.value))
except:
pass
adjusted_width = (max_length + 2)
ws.column_dimensions[column].width = adjusted_width
# 设置单元格边框和对齐方式
thin_border = Border(left=Side(style='thin'),
right=Side(style='thin'),
top=Side(style='thin'),
bottom=Side(style='thin'))
for row in ws.iter_rows(min_row=2, max_row=ws.max_row, min_col=1, max_col=ws.max_column):
for cell in row:
cell.border = thin_border
cell.alignment = Alignment(horizontal="center", vertical="center")
wb.save(filename)
def main():
input_filename = "考场安排demo.xlsx"
total_halls = 24
df = load_data(input_filename)
arranged_students = assign_seats(df, total_halls)
arranged_df = pd.DataFrame(arranged_students)
current_time = datetime.now().strftime("%Y%m%d_%H%M%S")
output_filename = f"考场安排结果_{current_time}.xlsx"
save_data(arranged_df, output_filename)
print(f"已成功生成文件:{output_filename}")
if __name__ == "__main__":
main()
美化步骤:
-
设置标题行样式:
-
使用
Font
来设定字体加粗和颜色。 -
使用
PatternFill
来设定标题行的背景颜色。 -
使用
Alignment
来设置标题单元格内容居中。
-
-
自动调整列宽:
- 遍历每一列,计算列中最长的单元格内容长度,并据此设置列宽,使得内容完整显示。
-
设置单元格边框和对齐方式:
-
使用
Border
和Side
来为每个单元格加上细边框。 -
对每个单元格设置居中对齐
Alignment
。
-
这样设置后,生成的Excel文件在视觉上会更加整洁和美观,并且更符合传统表格的排版格式。可以在正式运行前用少量数据进行测试。
完整示例代码如下:
-
引入
openpyxl
:添加import openpyxl
。 -
设置适当的页边距、行高和打印设置。
-
在代码最后保存并提示用户文件已生成。
bash
import pandas as pd
import random
import openpyxl
from openpyxl.styles import Font, Alignment, PatternFill, Border, Side
from openpyxl.utils import get_column_letter
from openpyxl.worksheet.page import PageMargins
from datetime import datetime
def load_data(filename):
return pd.read_excel(filename)
def save_data(df, filename):
df.to_excel(filename, index=False)
format_excel(filename)
def assign_seats(df, total_halls):
grouped = df.groupby('班级')
groups = [group.sample(frac=1).reset_index(drop=True) for _, group in grouped]
iterators = [iter(group.iterrows()) for group in groups]
arranged_data = []
hall_number = 1
seat_number = 1
total_students = len(df)
students_per_hall = total_students // total_halls
extra_students = total_students % total_halls
while iterators:
random.shuffle(iterators)
for it in iterators[:]:
try:
_, student = next(it)
student_data = student.to_dict()
student_data["考场"] = hall_number
student_data["考号"] = f"{seat_number:02d}"
arranged_data.append(student_data)
seat_number += 1
if seat_number > students_per_hall + (1 if hall_number <= extra_students else 0):
hall_number += 1
seat_number = 1
if hall_number > total_halls:
hall_number = 1
except StopIteration:
iterators.remove(it)
return arranged_data
def format_excel(filename):
wb = openpyxl.load_workbook(filename)
ws = wb.active
# 设置标题行的样式
for cell in ws[1]:
cell.font = Font(bold=True, color="FFFFFF")
cell.fill = PatternFill(start_color="4F81BD", end_color="4F81BD", fill_type="solid")
cell.alignment = Alignment(horizontal="center", vertical="center")
# 自动调整列宽
for col in ws.columns:
max_length = 0
column = col[0].column_letter
for cell in col:
try:
if len(str(cell.value)) > max_length:
max_length = len(str(cell.value))
except:
pass
adjusted_width = (max_length + 2)
ws.column_dimensions[column].width = adjusted_width
# 设置行高(适当调整以适应A4纸)
for row in range(1, ws.max_row + 1):
ws.row_dimensions[row].height = 18 # 行高设置为18像素
# 设置单元格边框和对齐方式
thin_border = Border(left=Side(style='thin'),
right=Side(style='thin'),
top=Side(style='thin'),
bottom=Side(style='thin'))
for row in ws.iter_rows(min_row=2, max_row=ws.max_row, min_col=1, max_col=ws.max_column):
for cell in row:
cell.border = thin_border
cell.alignment = Alignment(horizontal="center", vertical="center")
# 设置页边距
ws.page_margins = PageMargins(left=0.5, right=0.5, top=0.5, bottom=0.5)
# 设置打印区域(每张A4纸显示一个考场)
total_halls = 24
total_students = ws.max_row - 1 # 排除标题行
students_per_hall = total_students // total_halls
extra_students = total_students % total_halls
for hall_number in range(1, total_halls + 1):
min_row = 2 + (hall_number - 1) * (students_per_hall + (1 if hall_number <= extra_students else 0))
max_row = min_row + students_per_hall + (1 if hall_number <= extra_students else 0) - 1
ws.print_area = f"A1:E{max_row}"
# 配置页面设置
ws.page_setup.orientation = 'portrait'
ws.page_setup.paperSize = ws.PAPERSIZE_A4
ws.page_setup.fitToWidth = True
ws.page_setup.fitToHeight = False
wb.save(filename)
def main():
input_filename = "考场安排demo.xlsx"
total_halls = 24
df = load_data(input_filename)
arranged_students = assign_seats(df, total_halls)
arranged_df = pd.DataFrame(arranged_students)
current_time = datetime.now().strftime("%Y%m%d_%H%M%S")
output_filename = f"考场安排结果_{current_time}.xlsx"
save_data(arranged_df, output_filename)
print(f"已成功生成文件:{output_filename}")
if __name__ == "__main__":
main()
关键美化与打印设置说明:
-
自动调整列宽:根据单元格内容自动调整列宽,以适应打印输出。
-
设置标题行的样式:加粗并设置标题行字体颜色和背景颜色,使其与数据行区别开来。
-
设置行高:设置统一的行高(18像素)以适应A4纸打印要求。
-
设置单元格边框和对齐方式:所有单元格增加边框和居中对齐。
-
设置页边距:为页面设置合适的页边距(0.5英寸)。
-
设置打印区域:每个考场的安排显示在一张A4纸上。
现在,运行程序将生成已格式化和打印优化的Excel文件,包含每个考场的安排,并输出为设置在A4纸上的页边距和打印区域。
运行后的结果:
这里展示了两个考场,相邻的两个考生看上去来至于不同的班级。由于各班级人数不等,有的班级人数过多,最后一个考场有几个相同班的学生连在一起。
如何解决这个问题?
bash
请通过一个函数检查最后输出的结果,如果有相邻的考生来至于同一个班级,请参照前面的安排,适当的调整,全部安排,保证相邻的考生来至于不同的班级
要确保没有相邻的考生来自同一个班级,我们需要一个后处理函数来检查最终的座位安排,并在发现问题时进行调整。以下是一个改进方案,它在安排考位之后,通过检查并调整相邻考生的班级来防止同一班级的考生相邻:
"将相邻的学生和随后找到的另一个班级的学生交换位置", 这个算法不完全正确。随后仍然可能是一个班级,应该是从前面已排的学生中寻找满足以下条件的记录进行调换:这个学生前后都与调整的学生班级不一样
现在给出的结果,没有连续几个来至同一个班级的考生了,但还是有相邻两个来至于同一个班,请调整后,重新调用检查函数,直到不再有相邻学生来至于一个班级
为了确保相邻的学生不来自同一个班级,我们可以用一个循环不断地检查并调整座位安排,直到没有相邻考生来自同一个班级为止。我们可以在`check_and_adjust_seating`函数中用一个布尔值标记调整是否完成,如果发现有相邻的学生来自同一个班级,则进行调整并再次检查。
### 改进内容:
1. 修改了`check_and_adjust_seating`函数,确保在调整座位后再一次检查是否有相邻的考生来自同一个班级。如果有相邻的考生来自同一个班级,则继续调整,直到没有相邻的考生来自同一个班级。
2. 用一个布尔值`adjusted`跟踪是否进行了调整,如果进行了调整则继续检查,否则结束循环。
这样可以确保最终输出的结果中没有相邻两个考生来自同一个班级。
调整后,应该反复从开始验证,最终确保最终输出的结果中没有相邻两个考生来自同一个班级,只给出调整函数
最后结果
看上去已经没有相邻两个考生来自同一个班级,实际上gpt只是调整记录位置,考场号和座位号还是原来的值,让gpt从新分配考场号和座位号
import pandas as pd
import random
import openpyxl
from openpyxl.styles import Font, Alignment, PatternFill, Border, Side
from openpyxl.worksheet.page import PageMargins
from datetime import datetime
def load_data(filename):
return pd.read_excel(filename)
def save_data(df, filename):
df.to_excel(filename, index=False)
format_excel(filename, df)
def assign_seats(df, total_halls):
grouped = df.groupby('班级')
groups = [group.sample(frac=1).reset_index(drop=True) for _, group in grouped]
# Sort groups by size (descending)
groups.sort(key=lambda x: len(x), reverse=True)
iterators = [iter(group.iterrows()) for group in groups]
arranged_data = []
hall_number = 1
seat_number = 1
total_students = len(df)
students_per_hall = total_students // total_halls
extra_students = total_students % total_halls
while iterators:
# Shuffle iterators to ensure randomness
random.shuffle(iterators)
for it in iterators[:]:
try:
_, student = next(it)
student_data = student.to_dict()
student_data["考场"] = hall_number
student_data["考号"] = f"{seat_number:02d}"
arranged_data.append(student_data)
seat_number += 1
# Check if seat number exceeds the allowed number of students per hall
if seat_number > students_per_hall + (1 if hall_number <= extra_students else 0):
hall_number += 1
seat_number = 1
# Reset hall number if it exceeds total halls
if hall_number > total_halls:
hall_number = 1
except StopIteration:
iterators.remove(it)
return arranged_data
def check_and_adjust_seating(arranged_data):
def has_adjacent_same_class(data):
# Check if there are any adjacent students from the same class
for i in range(len(data) - 1):
if data[i]['班级'] == data[i + 1]['班级']:
return i
return -1
def find_valid_swap(index, data):
# Find a valid swap by checking front part of the list before the index
current_class = data[index]['班级']
for j in range(len(data)):
if j != index and data[j]['班级'] != current_class:
# Ensure new surrounding are valid
if (j == 0 or data[j - 1]['班级'] != current_class) and (
j == len(data) - 1 or data[j + 1]['班级'] != current_class):
return j
return -1
swap_operations = []
while True:
index = has_adjacent_same_class(arranged_data)
if index == -1:
break # No adjacent students from the same class
swap_index = find_valid_swap(index + 1, arranged_data)
if swap_index == -1:
raise ValueError("Cannot find a valid swap to adjust the seating arrangement.")
# Record the swap operation
swap_operations.append((index + 1, swap_index))
# Swap the adjacent student with the valid student
arranged_data[index + 1], arranged_data[swap_index] = arranged_data[swap_index], arranged_data[index + 1]
# Additional check and swap if any, continue from the start
while has_adjacent_same_class(arranged_data) != -1:
index = has_adjacent_same_class(arranged_data)
if index != -1:
swap_index = find_valid_swap(index, arranged_data)
if swap_index != -1:
# Record the swap operation
swap_operations.append((index, swap_index))
arranged_data[index], arranged_data[swap_index] = arranged_data[swap_index], arranged_data[index]
return arranged_data, swap_operations
def reassign_seats(arranged_data, total_halls):
hall_number = 1
seat_number = 1
total_students = len(arranged_data)
students_per_hall = total_students // total_halls
extra_students = total_students % total_halls
for i, student in enumerate(arranged_data):
student['考场'] = hall_number
student['考号'] = f"{seat_number:02d}"
seat_number += 1
if seat_number > students_per_hall + (1 if hall_number <= extra_students else 0):
hall_number += 1
seat_number = 1
if hall_number > total_halls:
hall_number = 1
return arranged_data
def format_excel(filename, df):
wb = openpyxl.load_workbook(filename)
ws = wb.active
# 设置标题行的样式
for cell in ws[1]:
cell.font = Font(bold=True, color="FFFFFF")
cell.fill = PatternFill(start_color="4F81BD", end_color="4F81BD", fill_type="solid")
cell.alignment = Alignment(horizontal="center", vertical="center")
# 自动调整列宽
for col in ws.columns:
max_length = 0
column = col[0].column_letter
for cell in col:
try:
if len(str(cell.value)) > max_length:
max_length = len(str(cell.value))
except:
pass
adjusted_width = (max_length + 20)
ws.column_dimensions[column].width = adjusted_width
# 设置行高(适当调整以适应A4纸)
for row in range(1, ws.max_row + 1):
ws.row_dimensions[row].height = 23 # 行高设置为23像素
# 设置单元格边框和对齐方式
thin_border = Border(left=Side(style='thin'), right=Side(style='thin'), top=Side(style='thin'),
bottom=Side(style='thin'))
for row in ws.iter_rows(min_row=2, max_row=ws.max_row, min_col=1, max_col=ws.max_column):
for cell in row:
cell.border = thin_border
cell.alignment = Alignment(horizontal="center", vertical="center")
# 设置页边距
ws.page_margins = PageMargins(left=0.5, right=0.5, top=0.5, bottom=0.5)
hall_groups = df.groupby('考场')
# 设置重复打印标题行
ws.print_title_rows = '1:1'
ws.print_title_cols = None
# 逐个考场设置打印区域
for hall_number, hall_df in hall_groups:
min_row = hall_df.index[0] + 2 # 加2以跳过标题行和数据框索引偏移
max_row = hall_df.index[-1] + 2 # 加2以跳过标题行和数据框索引偏移
# 初始化 print area, configuration of page settings for each hall
ws.print_area = f"A1:E{max_row}"
# 配置页面设置
ws.page_setup.orientation = 'portrait'
ws.page_setup.paperSize = ws.PAPERSIZE_A4
ws.page_setup.fitToPage = True
ws.page_setup.fitToWidth = 1
ws.page_setup.fitToHeight = False # 允许纵向页面数自动扩展
wb.save(filename)
def main():
input_filename = "11-15.xlsx" # 修改为学生信息的电子表格名
total_halls = 9 # 修改为考场数
df = load_data(input_filename)
arranged_students = assign_seats(df, total_halls)
adjusted_students, swap_operations = check_and_adjust_seating(arranged_students)
final_students = reassign_seats(adjusted_students, total_halls)
arranged_df = pd.DataFrame(final_students)
current_time = datetime.now().strftime("%Y%m%d_%H%M%S")
output_filename = f"考场安排结果_{current_time}.xlsx"
save_data(arranged_df, output_filename)
print(f"已成功生成文件:{output_filename}")
if __name__ == "__main__":
main()
最后在Microsoft Excel 中检测相邻单元格值是否相等
-
选择数据区域:
- 选择你想要检查的单元格区域。例如,假设你要检查从A2到A100的单元格。
-
打开条件格式:
- 在Excel的"开始"选项卡上,点击"条件格式"。
-
创建新的规则:
- 在"条件格式"下拉菜单中,选择"新建规则"。
-
使用公式来检测:
-
在"选择规则类型"窗口,选择"使用公式确定要设置格式的单元格"。
-
在"为符合此公式的值设置格式"框中输入适当的公式。例如,如果你检查A列中的相邻单元格,可以使用公式:
=A2=A1
-
然后你可以设置条件匹配时应用的格式,如填充颜色或字体颜色等。
-
-
应用条件格式:
- 设置好格式后,点击"确定"。这个操作将会在指定区域内自动应用条件格式。如果两个相邻单元格的值相同,它们将会被醒目地突出显示。
程序已完全符合要求。
食用方法
确保你安装了所需库:
bash
pip install pandas openpyxl numpy
1、表结构
2、修改input_filename的值 ,此变量存储的是学生信息的电子表格名,扩展名为.xlsx
3、修改total_halls的值,此变量存储的是考场个数
4、如果要分类安排考场,可将各层次学生分别建立对应的电子表格文件。
运行程序后,将生成一个已经格式化美化好的Excel文件。
如果有好的建议欢迎交流!!!,食用过程中错误请评论区给出!!!