避免在xlsxwritter中重复代码

时间:2015-07-21 19:37:42

标签: xlsxwriter

我想用xlsxwritter创建时间表,但是我找到了一些过程,并且会像下面的代码那样长得很长:

worksheet.set_column('B6:B6', 3)
worksheet.write('C9', "Day")
worksheet.write('C7', "Date")
worksheet.write('E7', "Start")
worksheet.write('F7', "End")

worksheet.write('C7', "SUN")
worksheet.write('C8', "MON")
worksheet.write('C9', "TUE")
worksheet.write('C10', "WED")
worksheet.write('C11', "THU")
worksheet.write('C12', "FRI")
worksheet.write('C13', "SAT")

worksheet.write('J7', "SUN")
worksheet.write('J8', "MON")
worksheet.write('J9', "TUE")
worksheet.write('J10', "WED")
worksheet.write('J11', "THU")
worksheet.write('J12', "FRI")
worksheet.write('J13', "SAT")

worksheet.write('N7', "SUN")
worksheet.write('N8', "MON")
worksheet.write('N9', "TUE")
worksheet.write('N10', "WED")
worksheet.write('N11', "THU")
worksheet.write('N12', "FRI")
worksheet.write('N13', "SAT")

结果像在pic中那样: enter image description here

问题:有没有办法实现这样的任务而不必重复代码>?

1 个答案:

答案 0 :(得分:0)

这适合您的需要吗?

您只需要调整SOME_MAX1和SOME_MAX2变量

days = ["SUN","MON","TUE","WED","THU","FRI","SAT"]
rows = range(7,SOME_MAX1)
cols = range(3,SOME_MAX2,10) + range(9,SOME_MAX2,10)

for r in rows:
    for c in cols:
        worksheet.write( r, c, days[ r % 7 ] )

编辑:白天,日期,开始,结束:你可以做类似的事情