zoukankan      html  css  js  c++  java
  • python生成excel格式座位表

    脚本分两个文件:

    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打包

            

  • 相关阅读:
    帮助应届生、年轻程序员快速成长的12个锦囊<转载>
    曾国藩教您:如何富过三代
    Mvc示例代码调试之二——调试示例讲解
    如何处理婚姻问题
    jQuery对象与dom对象相互转换
    Mvc示例代码调试之一----调试工具及设置(用firebug与vs联合调试)
    Mvc示例之三——用Filter进行简单身份验证
    沟通的艺术
    做bs开发需要学习哪些技术
    我所读过的技术书籍
  • 原文地址:https://www.cnblogs.com/langyao/p/7216952.html
Copyright © 2011-2022 走看看