鱼C论坛

 找回密码
 立即注册
查看: 9831|回复: 2

[作品展示] 制作无课表(一只Python菜鸟写的小程序)

[复制链接]
发表于 2018-3-8 17:52:47 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 苏丛 于 2018-3-8 18:04 编辑

准备工作:安装xlrd, xlwt模块, 一些个人无课表

操作方法:将个人无课表与小程序放在同一个文件夹里,运行程序

注意事项:个人无课表的格式要符合模板的, 文件名为:名字.xlsx (eg 苏丛.xlsx)

模板如图
无标题.jpg

代码如下
  1. import os
  2. import xlrd
  3. import xlwt

  4. #打开当前目录
  5. all_files = os.listdir(os.curdir)
  6. #用来收集.xlsx文件的列表
  7. file = []
  8. target = '无课'
  9. loc_dict = {}

  10. #获得无课的坐标和课表所有人
  11. def get_loc():
  12.     #收集.xlsx文件
  13.     for each_file in all_files:
  14.         if os.path.splitext(each_file)[1] == '.xlsx':
  15.             file.append(each_file)

  16.     #收集坐标和名字数据
  17.     for each_file in file:
  18.         name = os.path.splitext(each_file)[0]
  19.         workbook = each_file
  20.         #打开文件
  21.         with xlrd.open_workbook(workbook) as wb:
  22.             #打开第一个表格
  23.             sheet = wb.sheets()[0]

  24.             #遍历表格,查找目标
  25.             for row_num in range(sheet.nrows):
  26.                 for col_num in range(sheet.ncols):
  27.                     #记录坐标和名字数据
  28.                     if sheet.cell_value(row_num, col_num) == target:
  29.                         try:
  30.                             loc_dict[row_num, col_num] += ' ' + name
  31.                         except KeyError:
  32.                             loc_dict[row_num, col_num] = name

  33. def get_frame(sheet):
  34.     #获得课表框架
  35.     with xlrd.open_workbook(file[0]) as frame:
  36.         sheet = frame.sheets()[0]
  37.         frame_col = sheet.col_values(0)
  38.         frame_row = sheet.row_values(0)

  39.     return frame_col, frame_row

  40. def write_name(sheet):
  41.     #写入姓名
  42.     for each_loc in loc_dict:
  43.         #四个参数分别是纵坐标,横坐标,名字
  44.         sheet.write(each_loc[0], each_loc[1], loc_dict[each_loc])

  45. #写入框架
  46. def write_frame(frame_col, frame_row, sheet):
  47.     loc = 0
  48.     for each_row in frame_row:
  49.         sheet.row(0).write(loc, each_row)
  50.         loc += 1
  51.         
  52.     loc = 0
  53.     #这里不知道为什么不能跟上面写入一行一样地写入一列。
  54.     for each_col in frame_col:
  55.         sheet.write(loc, 0, each_col)
  56.         loc += 1

  57. def write_workbook():
  58.     #创建一个新的课表
  59.     new_wb = xlwt.Workbook()
  60.     sheet = new_wb.add_sheet('无课表', cell_overwrite_ok = True)

  61.     frame_col, frame_row = get_frame(sheet)

  62.     write_frame(frame_col, frame_row, sheet)

  63.     write_name(sheet)

  64.     new_wb.save('无课表.xls')

  65. if __name__ == '__main__':
  66.     get_loc()
  67.     write_workbook()
  68.    
复制代码


还有一些问题:
不能保存为.xlsx文件
我在想加一个检测功能:检测出不符合模板的文件,然后生成一个txt文件。但还没想好怎么写。

还请在座的各位大佬不吝赐教!
有任何做的不好的都提出来,小毛病大毛病都行,或者有哪些不好的编程习惯都可以指出来
嘻嘻嘻


小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-5-8 09:27:49 | 显示全部楼层

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-5-13 16:40:28 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-21 07:55

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表