1 #-*- coding: UTF-8 -*- 2 import xlrd 3 import sys 4 import locale 5 import os 6 import xlwt 7 from xlutils.copy import copy 8 9 reload(sys) 10 sys.setdefaultencoding( "utf-8" ) 11 homedir=os.getcwd() 12 print u"文件路径",homedir 13 print u"""执行文件前首先阅读以下说明文档: 14 1.首先请把要换班的excel文件转换为xls格式,即文件打开,点击文件选项,点击另存为并选择格式为xls 15 (可以用xlsx尝试以下,有的时候会出错) 16 2.确定文件sheet的名称为"班表",并且sheet位置排在首位 17 3.把xls文件放在文件路径下面,即和程序放在一个目录里 18 4.每次的修改完成的记录会在本目录下xiugai.txt文件里面 19 """ 20 21 def get_col_number(excelname,sheetname,index,name): 22 fileHandler = xlrd.open_workbook(excelname) 23 page = fileHandler.sheet_by_name(sheetname) 24 col1 = page.col_values(index) 25 for item in col1: 26 if item ==name: 27 nrow=col1.index(item) 28 return nrow 29 def changge_worktable(excelname,sheetname,sheetnumber,number1,number2,date): 30 fileHandler = xlrd.open_workbook(excelname) 31 page = fileHandler.sheet_by_name(sheetname) 32 cell_value1= page.cell(number1,date).value 33 cell_value2= page.cell(number2,date).value 34 newwb = xlrd.open_workbook(excelname, formatting_info=True) # formatting_info 带格式导入 35 outwb = copy(newwb) # 建立一个副本来用xlwt来写 36 # 修改值 37 def setOutCell(outSheet, col, row, value): 38 def _getOutCell(outSheet, colIndex, rowIndex): 39 row = outSheet._Worksheet__rows.get(rowIndex) 40 if not row: return None 41 cell = row._Row__cells.get(colIndex) 42 return cell 43 previousCell = _getOutCell(outSheet, col, row) 44 outSheet.write(row, col, value) 45 if previousCell: 46 newCell = _getOutCell(outSheet, col, row) 47 if newCell: 48 newCell.xf_idx = previousCell.xf_idx 49 outSheet = outwb.get_sheet(sheetnumber) 50 setOutCell(outSheet, date,number1,cell_value2) 51 setOutCell(outSheet, date, number2,cell_value1) 52 outwb.save(excelname) 53 54 def main(): 55 excelname=raw_input(u"请输入excelname(带文件类型名):".encode(‘utf-8‘)).decode(sys.stdin.encoding or locale.getpreferredencoding(True)) 56 print excelname 57 sheetname=u"班表" 58 sheetnumber=0 59 for i in range(0,500): 60 try: 61 name1=raw_input("name1:".encode(‘utf-8‘)).decode(sys.stdin.encoding or locale.getpreferredencoding(True)) 62 print name1 63 name2=raw_input("name2:".encode(‘utf-8‘)).decode(sys.stdin.encoding or locale.getpreferredencoding(True)) 64 print name2 65 datenumberlist=raw_input(u"请输入datenumber(多个的话用英文逗号隔开):").split(",") 66 print datenumberlist 67 68 number1=get_col_number(excelname,sheetname,1,name1) 69 number2=get_col_number(excelname,sheetname,1,name2) 70 for datenumber in datenumberlist: 71 72 changge_worktable(excelname,sheetname,sheetnumber,number1,number2,int(datenumber)+3) 73 print "i:",i 74 with open("xiugai.txt","a") as output: 75 output.write(name1+"***"+name2+"***"+str(datenumberlist)+"\r\n") 76 print "success" 77 except: 78 print "there is an error in your enter,please check and repeat the input again" 79 continue 80 esc=raw_input(u"想退出的话输入esc,继续的话按回车:".encode(‘utf-8‘)).decode(sys.stdin.encoding or locale.getpreferredencoding(True)) 81 if esc=="esc": 82 break 83 print "game is over" 84 85 if __name__ == "__main__": 86 main() 修改的excel格式图如下
时间: 2024-10-10 08:25:52