import xlsxwriter
workbook = xlsxwriter.Workbook('rry.xlsx')
worksheet = workbook.add_worksheet('2019年记帐')
worksheet2 = workbook.add_worksheet('2020年记帐')
worksheet.write('A1', '2019年收支表')
headings=['月份','收入','支出']
month = ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月' ]
incomes =[19000, 18000, 20000, 22000, 19000, 18000, 23000, 19000, 20000, 21000, 23000, 30000 ]
payouts = [12000 ,20000 ,8000 ,9000 ,10000 ,30000 ,20000 ,10000 ,5000 ,9000 ,10000 ,15000 ]
head_fmt = workbook.add_format({
'bold': True,
'bg_color': '#4076CE',
'align': 'center',
'valign': 'vcenter',
'border': 1,
'font': '楷体',
'font_size': 12,
'font_color': 'white'
})
worksheet.write_row('A2', headings, head_fmt)
month_fmt = workbook.add_format({
'border': 1,
'bg_color': '#B8CFEF',
'align': 'center',
'font': '宋体',
'font_size': 11
})
worksheet.write_column('A3', month, month_fmt)
border_fmt = workbook.add_format({
'border': 1
})
worksheet.write_column('B3', incomes, border_fmt)
worksheet.write_column('C3', payouts, border_fmt)
worksheet.set_column('B:C', 14)
worksheet.set_row(0, 30)
worksheet.write_string('A15', '小计', border_fmt)
worksheet.write_formula('B15', '=SUM(B3:B14)', border_fmt)
worksheet.write_formula('C15', '=SUM(C3:C14)', border_fmt)
worksheet.merge_range('E3:F6','合并后单元格内容')
worksheet.write_comment('C4', '这是我的备注。\n这笔支出不能告诉任何人')
chart1 = workbook.add_chart({'type':'column'})
chart1.add_series({
'name':'=2019年记帐!$B$2',
'categories':'=2019年记帐!$A$3:$A$14',
'values':'=2019年记帐!$B$3:$B$14'
})
chart1.add_series({
'name':'=2019年记帐!$C$2',
'values':'=2019年记帐!$C$3:$C$14'
})
chart1.set_title({'name':'柱状图'})
chart1.set_y_axis({'name':'收入/支出'})
chart1.set_x_axis({'name':'月份'})
chart1.set_style(11)
worksheet.insert_chart('E2',chart1)
workbook.close()