1. 程式人生 > >小興趣:用python生成excel格式座位表

小興趣:用python生成excel格式座位表

highlight rand end 正方 unit style over 思路 created

腳本分兩個文件:

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格式座位表