脚本分两个文件:

1.生成二维随机列表:GenerateLocaltion.py  2.将列表导入excel文件:CreateExcel.py

先上GenerateLocaltion.py:

import random
class Table(): localtion = [([1] * 9) for i in range(5)]
room = []
def inIt(self):
localtion = [([1] * 9) for i in range(5)]
for i in range(5):
localtion[i][0] = 0
#标志特殊位置
localtion[0][0] = 1
localtion[1][0] = 1
localtion[0][8] = 0
localtion[1][5] = 0
localtion[3] = [0] * 9
localtion[3][1] = 1
localtion[3][2] = 1
localtion[4][4] = 0
localtion[4][5] = 0
localtion[4] = [0] * 9
for i in range(5):
localtion[4][i] = 1 self.localtion = localtion
#生成随机列表
def generateRandomList(self):
#去掉空位
nothing = []
nothing.append(1)
nothing.append(13)
nothing.append(32)
nothing.append(31)
nothing.append(30)
nothing.append(29)
nothing.append(28)
nothing.append(27)
nothing.append(35)
nothing.append(36)
nothing.append(37)
nothing.append(38)
nothing.append(44) self.room = []
i = 0
while len(self.room) < 31:
m = int(random.random()*100 % 44 + 1)
if m not in self.room and m not in nothing:
self.room.append(m)
i += 1
return self.room
def generateLocal(self):
#随机列表对座位赋值
for i in range(5):
for j in range(9):
if self.localtion[i][j] == 1:
self.localtion[i][j] = self.room.pop(0)
return self.localtion
def getTable(self):
self.inIt()
self.generateRandomList() return self.generateLocal()

  代码很长,主要因为要特殊照顾一些位置,思路就是生成一个足够个数的随机序列(不能重复,不能有对应空位的座位号)往二维列表里塞,其他的都很简单

CreateExcel.py:

用xlwt模块和easygui模块

xlwt中调用的函数:

sheet.write_merge()函数:参数1,2,3,4可以理解为用行和列描述一块区域,前两个为行,后两个为列例如,i,i+n,j,j+n,边长为n的正方形

sheet.write()函数:向单元格中填充内容,参数1,2表示单元格坐标,参数3表示填充的内容,参数4是单元格格式

'''
Created on 2017年7月21日 @author: Garbos
'''
#coding:utf-8
import xlwt
import easygui as g
from GenerateLocaltion import Table as table
def setUnitStyle(name,height,bold=False):
style = xlwt.XFStyle() font = xlwt.Font()
font.name = name
font.bold = bold
font.color_index = 4
font.height = height
style.font = font return style def createExcel():
#創建工作簿
e = xlwt.Workbook() #創建表格sheet1
sheet1 = e.add_sheet(u'sheet1',cell_overwrite_ok=True) #創建第一行
sheet1.write_merge(0,0,0,3,u'',setUnitStyle('Times New Roman',500,False))
sheet1.write_merge(0,0,3,10,u'ACM 404集训座位表',setUnitStyle('Times New Roman',500,False))
sheet1.write_merge(1,1,1,4,u'',setUnitStyle('Times New Roman',300,False))
sheet1.write_merge(1,1,6,10,u'',setUnitStyle('Times New Roman',300,False))
sheet1.write(1,5,u'讲台',setUnitStyle(u'微软雅黑',400,True))
sheet1.write_merge(3,5,5,6,u'走廊',setUnitStyle('Times New Roman',800,False))
sheet1.write_merge(2,2,5,6,u'',setUnitStyle('Times New Roman',300,False))
sheet1.write_merge(6,6,5,6,u'',setUnitStyle('Times New Roman',300,False))
sheet1.write(1,0,u'门',setUnitStyle(u'微软雅黑',400,False))
gt = table()
t = gt.getTable() for i in range(5):
for j in range(9):
if t[i][j] == 0:
continue
temp = j
if temp >= 5:
temp += 2
sheet1.write(i+2,temp,t[i][j],setUnitStyle(u'微软雅黑',250,False))
filename = '404座位表.xls'
e.save(filename)#坑,xlsx无法打开 remind = g.msgbox(msg = filename + ' 已生成!',title='404座位表生成器',
ok_button = '取消')
if __name__ =='__main__':
createExcel()

 最后用Pyinstaller打包

        

python生成excel格式座位表的更多相关文章

  1. 小兴趣:用python生成excel格式座位表

    脚本分两个文件: 1.生成二维随机列表:GenerateLocaltion.py 2.将列表导入excel文件:CreateExcel.py 先上GenerateLocaltion.py: impor ...

  2. Python生成文本格式的excel\xlwt生成文本格式的excel\Python设置excel单元格格式为文本\Python excel xlwt 文本格式

    Python生成文本格式的excel\xlwt生成文本格式的excel\Python设置excel单元格格式为文本\Python excel xlwt 文本格式 解决: xlwt 中设置单元格样式主要 ...

  3. Html Table用JS导出excel格式问题 导出EXCEL后单元格里的000412341234会变成412341234 7-14 会变成 2018-7-14(7月14) 自定义格式 web利用table表格生成excel格式问题 js导出excel增加表头、mso-number-format定义数据格式 数字输出格式转换 mso-number-format:"\@"

    Html Table用JS导出excel格式问题 我在网上找的JS把HTML Tabel导出成EXCEL.但是如果Table里的数字内容为0开的的导成Excel后会自动删除0,我想以text的格式写入 ...

  4. python生成Excel图表(通过xlsxwriter)

    前面介绍了pandas的简单用法,pandas的数据可以通过matlab第三方包将数据生成报表,但是我想将报表生成在Excel中,这时候就可以借助xlsxwriter第三方包来生成图标   缺点:xl ...

  5. python 生成json格式文件,并存储到手机上

    上代码 #!/usr/bin/env python # -*- encoding: utf-8 -*- import json import os import random "" ...

  6. python生成excel测试数据

    在功能测试时,经常会测到excel文件导入导出的功能,导入与导出时,需要测试系统单次导入大批量数据时是否正常, 如果系统承受不起太大的数据导入,则需要开发限制单次导入的数量,以防止系统服务异常.大量的 ...

  7. web利用table表格生成excel格式问题

    当我们把web页面上的table导成excel形式时,有时候我们的数据需要以特定的格式呈现出来,这时候我们就需要给指定的单元格添加一些样式规格信息. 文本:vnd.ms-excel.numberfor ...

  8. (一)python 格式化 excel 格式

    需求: 客户通过 sftp 上传了一个 poc测试的 excel文件, 下到 云桌面 查看,发现一堆格式问题, 怎么办呢? 公司又不允许 吧文件下载到本地处理, 只能在 服务器上进行处理. 一堆的类型 ...

  9. python 生成excel,并下载到本地

    from django.shortcuts import reverse,redirect,render from operations import models import xlwt impor ...

随机推荐

  1. Yaml 的python 应用

    1.安装yaml的python包 2.准备yaml的数据 3.yaml.load 解析yaml 3.生产yaml

  2. Python 内建的filter()函数用于过滤序列。

    例如,在一个list中,删掉偶数,只保留奇数,可以这么写: def is_odd(n): return n % 2 == 1 list(filter(is_odd, [1, 2, 4, 5, 6, 9 ...

  3. 可执行jar包

    我已经解决了这个问题,在eclipse中有一个打包工具,可以将程序打包成.jar文件: 右键要打包的 project--->Export--->Java--->JAR file--- ...

  4. mysql 存储过程初探

    使用存储过程好处在于: 1.隐藏敏感的算法,避免被正常的开发人员看到,把业务逻辑隐藏在数据库中,而非程序代码里 2.简化应用代码程序,放到数据库里肯定就对程序代码简化有好处了 3.不同的开发语言都可以 ...

  5. AngularJS form $addControl 注冊控件control

    需求背景: 在form中使用编写的某component directive时.想通过form's name来对form中控件进行操作, 如使用$invalid等来ng-disabled btn. 解决 ...

  6. MATLAB循环结构:for语句+定积分实例

    for语句 格式: for 循环变量=表达式1:表达式2:表达式3 循环体语句 end 表达式1:循环变量初值:表达式2:步长:表达式3:循环变量终值. for 循环变量=矩阵表达式 循环体语句 en ...

  7. 4 Values whose Sum is 0(二分)

    4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 21370   Accep ...

  8. POJ 1113 Wall【凸包周长】

    题目: http://poj.org/problem?id=1113 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...

  9. 微信小程序报“app.json”错误解决办法

    1.亲测 “app.json未找到入口 app.json 文件,或者文件读取失败,请检查后重新编译.” 是由于新创建的界面xxx.json所在的文件夹为0KB造成的,你可以试着在xxx.json文件内 ...

  10. react-native 使用 antd-mobile-rn UI进行开发app

    1.创建 react-native 项目 react-native init app03 2.安装组件 npm install antd-mobile-rn --save 3.配置按需加载 npm i ...