1.新增表并添加数据;
2.给工作表添加表名称,给表数据添加格式;
import xlsxwriter
datas=(['Rent',1000],
['Gas',100],
['fish','画画'],
['rice',500])
a=xlsxwriter.Workbook('st.xlsx')
sh1=a.add_worksheet('表1') #添加工作表名称
money=a.add_format({'num_format':"$#,##0"}) #添加数字格式
bold=a.add_format({'bold':True}) #加粗
#添加表头,并加粗
sh1.write('A1','Item',bold)
sh1.write(0,1,'Cost',bold)
r,c=1,0
for names,costs in datas:
sh1.write(r,c,names)
sh1.write(r,c+1,costs,money) #给列添加数字格式
r+=1
sh1.write(r,c,'total',bold)
sh1.write(r,c+1,'=sum($B2:B5)',money)
a.close()