import xlsxwriter # 创建一个新的Excel文件 workbook = xlsxwriter.Workbook('weather.xlsx') # 创建一个工作表 worksheet = workbook.add_worksheet() # 设置列宽和行高 worksheet.set_column('A:A', 10) worksheet.set_column('B:B', 10) worksheet.set_column('C:C', 10) worksheet.set_column('D:D', 10) worksheet.set_column('E:E', 10) worksheet.set_column('F:F', 10) worksheet.set_column('G:G', 10) worksheet.set_column('H:H', 10) worksheet.set_column('I:I', 10) worksheet.set_column('J:J', 10) worksheet.set_column('K:K', 10) worksheet.set_column('L:L', 10) worksheet.set_column('M:M', 10) worksheet.set_column('N:N', 10) worksheet.set_column('O:O', 10) worksheet.set_column('P:P', 10) worksheet.set_column('Q:Q', 10) worksheet.set_column('R:R', 10) worksheet.set_column('S:S', 10) worksheet.set_column('T:T', 10) worksheet.set_column('U:U', 10) worksheet.set_column('V:V', 10) worksheet.set_column('W:W', 10) worksheet.set_column('X:X', 10) worksheet.set_column('Y:Y', 10) worksheet.set_column('Z:Z', 10) worksheet.set_row(0, 30) # 设置标题 title_format = workbook.add_format({'bold': True, 'font_size': 14}) worksheet.write('A1', '日期', title_format) worksheet.write('B1', '1月', title_format) worksheet.write('C1', '2月', title_format) worksheet.write('D1', '3月', title_format) worksheet.write('E1', '4月', title_format) worksheet.write('F1', '5月', title_format) worksheet.write('G1', '6月', title_format) worksheet.write('H1', '7月', title_format) worksheet.write('I1', '8月', title_format) worksheet.write('J1', '9月', title_format) worksheet.write('K1', '10月', title_format) worksheet.write('L1', '11月', title_format) worksheet.write('M1', '12月', title_format) # 写入数据 for i in range(1, 366): worksheet.write(i, 0, '第{}天'.format(i)) for j in range(1, 13): worksheet.write(i, j, i*j) # 创建一个柱形图 chart = workbook.add_chart({'type': 'column'}) # 设置图表数据范围 chart.add_series({ 'name': '1月', 'categories': '=Sheet1!$A$2:$A$366', 'values': '=Sheet1!$B$2:$B$366', }) chart.add_series({ 'name': '2月', 'categories': '=Sheet1!$A$2:$A$366', 'values': '=Sheet1!$C$2:$C$366', }) chart.add_series({ 'name': '3月', 'categories': '=Sheet1!$A$2:$A$366', 'values': '=Sheet1!$D$2:$D$366', }) chart.add_series({ 'name': '4月', 'categories': '=Sheet1!$A$2:$A$366', 'values': '=Sheet1!$E$2:$E$366', }) chart.add_series({ 'name': '5月', 'categories': '=Sheet1!$A$2:$A$366', 'values': '=Sheet1!$F$2:$F$366', }) chart.add_series({ 'name': '6月', 'categories': '=Sheet1!$A$2:$A$366', 'values': '=Sheet1!$G$2:$G$366', }) chart.add_series({ 'name': '7月', 'categories': '=Sheet1!$A$2:$A$366', 'values': '=Sheet1!$H$2:$H$366', }) chart.add_series({ 'name': '8月', 'categories': '=Sheet1!$A$2:$A$366', 'values': '=Sheet1!$I$2:$I$366', }) chart.add_series({ 'name': '9月', 'categories': '=Sheet1!$A$2:$A$366', 'values': '=Sheet1!$J$2:$J$366', }) chart.add_series({ 'name': '10月', 'categories': '=Sheet1!$A$2:$A$366', 'values': '=Sheet1!$K$2:$K$366', }) chart.add_series({ 'name': '11月', 'categories': '=Sheet1!$A$2:$A$366', 'values': '=Sheet1!$L$2:$L$366', }) chart.add_series({ 'name': '12月', 'categories': '=Sheet1!$A$2:$A$366', 'values': '=Sheet1!$M$2:$M$366', }) # 设置图表标题和坐标轴标签 chart.set_title({'name': '一年365天天气雨量柱形图'}) chart.set_x_axis({'name': '日期'}) chart.set_y_axis({'name': '雨量'}) # 将图表插入到工作表中 worksheet.insert_chart('O1', chart) # 关闭Excel文件 workbook.close()
Python XlsxWriter 一年365天天气雨量柱形图
最新推荐文章于 2024-04-16 23:19:57 发布