【python脚本】课表信息生成 course_test(源代码)

在处理excel表格数据中编写的一个可以自动处理课表信息的脚本。

import os
import openpyxl
import re

# 检查文件是否存在
file_name = "course_info.xlsx"
if os.path.exists(file_name):
    # 如果文件存在,先删除
    os.remove(file_name)

# 创建一个新的 Excel 工作簿
workbook = openpyxl.Workbook()
sheet = workbook.active
# 写入表头
sheet.append(["序号", "课程名称", "周次", "星期", "节次", "上课地点", "任课老师"])
# 保存 Excel 文件
workbook.save(file_name)

def append_data_to_excel(excel, new_data):
    # 打开已存在的 Excel 文件
    workbook = openpyxl.load_workbook(excel)
    # 选择要添加数据的工作表,这里假设工作表的名字是 "Sheet"
    sheet = workbook["Sheet"]
    # 准备要添加的新数据,例如一个列表
    # new_data = [num, CourseName, week, day, lesson, classroom, teacher]
    # 在工作表中添加新数据
    sheet.append(new_data)
    # 保存修改后的 Excel 文件
    workbook.save(excel)

# workbook = xlrd.open_workbook('./文献检测情况汇总(持续更新).xlsx')
workbook = openpyxl.load_workbook('./课表数据.xlsx')
work_sheet = workbook['data']

num = 0  # 初始化序号

for row in work_sheet.iter_rows(values_only=True, min_row=2,):
    CourseName = row[2];
    CoureseTimeAddress = row[25];
    # print(CourseName,CoureseTimeAddress)
    try:
        message = CoureseTimeAddress.split(';')
        # print(message)
    except:
        # print("no message")
        continue

    for m in message:
        if m == '':
            continue

        m = m.split()
        # print(m)
        week = m[0]
        day = m[1][:3]

        # 使用正则表达式提取方括号内的内容
        pattern = r'\[(.*?)\]'  # 匹配方括号内的任意字符,非贪婪模式
        matches = re.findall(pattern, m[1])
        lesson = matches[0]
        # print(lesson)

        # 使用正则表达式提取 "1-204" 模式
        pattern = r'\b\d+-\d+\b'  # 匹配数字-数字模式的单词边界
        matches = re.findall(pattern, m[1])
        classroom = matches[0]
        # print(classroom)

        teacher = m[2]

        # num = num + 1
        # print(num, CourseName, week, day, lesson, classroom, teacher)

        week = week.split(",")
        # print(week)

        for w in week:
            flag = 0
            if '单' in w:
                flag = 1
                if '周' not in w:
                    w = w[:-1]
                else:
                    w = w[:-2]
            elif '双' in w:
                flag = 2
                if '周' not in w:
                    w = w[:-1]
                else:
                    w = w[:-2]
            else:
                flag = 0
                if '周' in w:
                    w = w[:-1]

            # print(w)

            if '-' in w:
                a, b = w.split('-')

                try:
                    a, b = int(a), int(b)
                except:
                    print("error")
                    print(a,b)

                for i in range(a,b+1):
                    if flag==1 and i%2==0:
                        continue
                    if flag==2 and i%2!=0:
                        continue
                    num = num + 1
                    new_data = [num, CourseName, i, day, lesson, classroom, teacher]
                    print(num, CourseName, i, day, lesson, classroom, teacher)
                    # 将数据存入excel表格
                    append_data_to_excel("course_info.xlsx", new_data)
            else:
                num = num + 1
                w = int(w)
                new_data = [num, CourseName, w, day, lesson, classroom, teacher]
                print(num, CourseName, w, day, lesson, classroom, teacher)
                # 将数据存入excel表格
                append_data_to_excel("course_info.xlsx", new_data)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bosenya12

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值