之前幫別人產生測試報告時,用的都不是 python 語言,最近又幫忙產生別的測試報告,剛好對方近來比較常用 python,於是我也趁機試了一下 XlsxWriter,把一些常用語法記錄在這裡,下次再用就不用在專案資料夾裡翻找。
import xlsxwriter
def main():
output = 'example.xlsx'
wb = xlsxwriter.Workbook(output)
ws = wb.add_worksheet('Sheet1')
# freeze window
ws.freeze_panes(1, 1)
number_format = wb.add_format({'num_format': '00.00'})
number_format.set_font_color('red')
number_format.set_align('center')
title_format = wb.add_format()
title_format.set_align('center')
title_format.set_bold()
# title
row = 0
for col in range(8):
title = 'Title %d' % col
ws.write(row, col, title, title_format)
# write something
for row in range(1, 8):
for col in range(8):
s = '%02d' % (row * 10 + col)
ws.write(row, col, s, number_format)
wb.close()
if __name__ == '__main__':
main()
沒有留言:
張貼留言