当前位置: 首页 > 图灵资讯 > 行业资讯> python如何导出数据到excel文件

python如何导出数据到excel文件

发布时间:2025-02-27 18:34:55

Python将数据导出到excel文件的方法:

1、在对象中调用Workbook()add_sheet()方法

wb=xlwt.Workbook()
ws=wb.add_sheet('ATestSheet')

2、通过add_通过add_sheet()方法中的write()函数将数据写入excel,然后使用save()函数保存excel文件

ws.write(0,0,1234.56,style0)
ws.write(1,0,datetime.now(),style1)
ws.write(2,0,1)
ws.write(2,1,1)
ws.write(2,2,xlwt.Formula("A3+B3"))

wb.save('example.xls')

完整代码如下:

importxlwtfromdatetimeimportdatetime

style0=xlwt.easyxf('font:nameTimesNewRoman,color-indexred,boldon',num_format_str='#,##0.00')
style1=xlwt.easyxf(num_format_str='D-MMM-YY')

wb=xlwt.Workbook()
ws=wb.add_sheet('ATestSheet')

ws.write(0,0,1234.56,style0)
ws.write(1,0,datetime.now(),style1)
ws.write(2,0,1)
ws.write(2,1,1)
ws.write(2,2,xlwt.Formula("A3+B3"))

wb.save('example.xls')

程序执行结果如下:

091438322905708.png

更多Python知识,请关注:Python自学网!!

(推荐操作系统:windows7系统Python 3.9.1,DELL G3电脑。)

相关文章

python3兼容python2吗

python3兼容python2吗

2025-05-09
python3 whl怎么安装

python3 whl怎么安装

2025-05-09
python 字典怎么提取value

python 字典怎么提取value

2025-05-09
python 怎样计算字符串的长度

python 怎样计算字符串的长度

2025-05-09
python 怎么样反向输出字符串

python 怎么样反向输出字符串

2025-05-09
python 怎么判断字符串开头

python 怎么判断字符串开头

2025-05-09