脚本分两个文件:

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文件

    2018-04-1919:04:25 测试代码如下: import openpyxl import datetime wb = openpyxl.Workbook() ws = wb.active w ...

随机推荐

  1. 马踏棋盘算法递归+回溯法实现 C语言

    r为矩阵的行,c为矩阵的列 将结果输出到当前目录下的results.txt. 结果将给出:1.是否存在路径使马可以按要求走遍所有的方格: 2.解的总数: 3.程序执行的时间: #include< ...

  2. 【Android Developers Training】 79. 连接到网络

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  3. JS初步学习

    [使用JS的三种方式] 1.HTML标签中内嵌JS(不提倡使用): <button onclick="javascript:alert('小碧池!你真点啊!')">有本 ...

  4. pouchdb-find( pouchdb查询扩展插件 ,便于查询)

    pouchdb-find pouchdb-find 环境搭建 下载lib bower install pouchdb-find 引入js <script src="pouchdb.js ...

  5. diy toy: image auto-handler

    备忘之:) config.xml <?xml version="1.0" encoding="utf-8"?> <config> < ...

  6. org.apache.jasper.JasperException: - Page directive must not have multiple occurrences of pageencoding

    最近写jsp遇到一系列的低级错误,记录下来权当前车之鉴吧. 错误提示: SEVERE: Servlet.service() for servlet jsp threw exceptionorg.apa ...

  7. PHP获取文件夹中的所有文件(包括子目录)

    方法一: 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 function tree($directory) ...

  8. CSS 样式书写规范+特殊符号

    虽然我只是刚踏入web前端开发圈子.在一次次任务里头,我发觉每一次的css命名都有所不同和不知所措.脑海就诞生了一个想法--模仿大神的css命名样式. 毕竟日后工作上,是需要多个成员共同协作的.如果没 ...

  9. 关于Verilog HDL的一些技巧、易错、易忘点(不定期更新)

    本文记录一些关于Verilog HDL的一些技巧.易错.易忘点等(主要是语法上),一方面是方便自己忘记语法时进行查阅翻看,另一方面是分享给大家,如果有错的话,希望大家能够评论指出. 关键词: ·技巧篇 ...

  10. 一个简单的java贷款程序

    代码如下: //计算贷款package ClassDemo;import javax.swing.JOptionPane; public class ComputeLoan { public stat ...