The script is divided into two files:
1. Generate a two-dimensional random list: generatelocaltion.py 2. Import a list into an Excel file: createexcel.py
First on generatelocaltion.py:
Import Randomclass Table (): Localtion = [([1] * 9) for I in range (5)] [+] def inIt (self): Localt Ion = [([1] * 9) for I in range (5)] for I in range (5): localtion[i][0] = 0 #标志特殊位置 localti On[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 Local TION[4] = [0] * 9 for I in range (5): localtion[4][i] = 1 self.localtion = localtion #生 into a random list def generaterandomlist (self): #去掉空位 nothing = [] nothing.append (1) nothing.append (13) Nothing.append (Nothing.append) nothing.append (+) nothing.append (Nothing.ap) Pend (Nothing.append) nothing.append (+) Nothing.append (+) nothing.append (PNS) No Thing.append (38)Nothing.append (Self.room) = [] i = 0 while Len (self.room) < 31:m = Int (rand Om.random () *100% + 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 () r Eturn self.generatelocal ()
Code is very long, mainly because to take special care of some locations, the idea is to generate a sufficient number of random sequences (can not be duplicated, there is no seat number corresponding to the vacancy) to the two-dimensional list plug, the others are very simple
createexcel.py:
With XLWT module and Easygui module
Functions that are called in XLWT:
Sheet.write_merge () function: parameter 1,2,3,4 can be understood to describe an area with rows and columns, the first two rows, and the last two columns for example, I,i+n,j,j+n, and a square with an edge length of n
Sheet.write () Function: Fills the cell with the contents, the parameter is the cell coordinates, parameter 3 indicates the contents of the fill, parameter 4 is the cell format
"' Created on July 21, 2017 @author: Garbos ' #coding: Utf-8import xlwtimport Easygui as gfrom generatelocaltion import Table As TableDef 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_merg E (0,0,0,3,u ", Setunitstyle (' Times New Roman ', 500,false)) Sheet1.write_merge (0,0,3,10,u ' ACM 404 training Seating table ', 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 ' podium ', Setunitstyle (U ' Microsoft Ya Black ', 400, True) Sheet1.write_merge (3,5,5,6,u ' Corridor ', 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)) sheet 1.write (1,0,u ' Gate ', Setunitstyle (U ' Microsoft Jas Black ', 400,false)) GT = table () T = gt.gettable () for I in range (5): fo R 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 ' Microsoft Ya Black ', 250,false)) filename = ' 404 seating table. xls ' E.save (filename) #坑, xlsx cannot open remind = g.msgbox (msg = filename + ' generated! ', title= ' 404 Seating Chart generator ', Ok_button = ' Cancel ') if __name__ = = ' __main__ ': Createexcel ()
Finally packed with Pyinstaller.
Small interest: Generate table of seats in Excel format with Python