一:安装模块xlsxwriter
file-settings-project intepreter-点加号或者pip查找模块-安装
二:基础语句
1.import xlsxwriter:载入模块 2.workbook = xlsxwriter.Workbook(‘abc.xlsx’):新建名为abc的excel文件 3.worksheet = workbook.add_worksheet(‘def’):新建名为def的表 4.worksheet.write(‘A1’,‘100’):在A1写入100,只能单个写入 5.worksheet.write_row(‘A1’,a):按行逐一写入列表a,多个写入 6.worksheet.write_column(‘A1’,b):按行逐一写入列表b,多个写入 7.wrokbook.close():关闭并保存文件
三:注意事项
1.work极易打错:wrok 2.写入数字时不加’ ',字符串格式无法做运算
四:easy demo
import xlsxwriter
datas
=(
['rent',1000],
['gas',120],
['food',300],
['gym',50],
['app',600],
['lemom',5000]
)
workbook
= xlsxwriter
.Workbook
('excel01.xlsx')
worksheet
= workbook
.add_worksheet
()
row
, col
= 0, 0
for item
,cost
in datas
:
worksheet
.write
(row
,col
,item
)
worksheet
.write
(row
,col
+1,cost
)
row
+=1
worksheet
.write
(row
,0,'total')
worksheet
.write
(row
,1,'=sum(B1:B6)')
workbook
.close
()
import xlsxwriter
head
= ['姓名','分数']
name
= ['张三','李四','王五','老六']
mark
= [1,2,3,4]
workbook
= xlsxwriter
.Workbook
('chengji.xlsx')
worksheet
= workbook
.add_worksheet
()
worksheet
.write_row
('A1',head
)
worksheet
.write_column
('A2',name
)
worksheet
.write_column
('B2',mark
)
worksheet
.write
('A6','total')
worksheet
.write
('B6','=sum(B2:B5)/4')
workbook
.close
()