1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| import xlsxwriter
workbook = xlsxwriter.Workbook('test.xlsx')
worksheet1 = workbook.add_worksheet('test-sheet1')
row_data=['姓名','数学','英语','语文'] col_data = [ ['张三','李四','老王','德华','赵四'], [90, 85, 120, 130, 99], [70, 65, 120, 109, 110], [60, 95, 130, 120, 79] ] worksheet1.write_row('A1',row_data) worksheet1.write_column('A2',col_data[0]) worksheet1.write_column('B2',col_data[1]) worksheet1.write_column('C2',col_data[2]) worksheet1.write_column('D2',col_data[3])
chart = workbook.add_chart({'type':'line'})
chart.add_series({ 'name':'=test-sheet1!$B$1', 'categories': '=test-sheet1!$A$2:$A$6', 'values': '=test-sheet1!$B$2:$B$6', 'line': {'color': 'red'}, 'pie': {'color': 'red'}, }) chart.add_series({ 'name':'=test-sheet1!$C$1', 'categories': '=test-sheet1!$A$2:$A$6', 'values': '=test-sheet1!$C$2:$C$6', 'line': {'color': 'yellow'}, 'pie': {'color': 'yellow'}, }) chart.add_series({ 'name':'=test-sheet1!$D$1', 'categories': '=test-sheet1!$A$2:$A$6', 'values': '=test-sheet1!$D$2:$D$6', 'line': {'color': 'blue'}, 'pie': {'color': 'blue'}, }) chart.set_title({'name':'测试'}) chart.set_x_axis({'name':"x轴"}) chart.set_y_axis({'name':'y轴'}) chart.set_style(1)
worksheet1.insert_chart('B8',chart,{'x_offset':25,'y_offset':10}) workbook.close() """ 官方文档:https://xlsxwriter.readthedocs.io/chart.html """
|