python操作excel文件有多种方式,这里主要介绍使用xlwt操作excel文件
一、介绍xlwt
参见 :https://github.com/python-excel,使用xlwt可以操作与MS Excel 97/2000/XP/2003 XLS files兼容的excel文件。不兼容Excel2007/2010等 XLSX等格式的文件。
二、安装xlwt
pip install xlwt
三、主要 API介绍:
1、在未介绍xlwt主要api之前,先介绍几点注意事项:
1)如果使用editplus等编辑器编写python文件,保存文件时注意编码格式,要选用utf-8编码格式,否在在命令行执行时会报编码错误;
2)主要在python源文件中Tab制表符要用空格代替;
3)打开文件时,如果提示编码不对,需要使用二进制流的方式,比如 open('c:\test.bmp','rb')
4)写入excel汉字乱码时,需要使用utf-8子附件,比如Workbook(encoding='utf-8')
2、xlwt 设置单元格格式方法
1)Borders()
border0 = Borders()
border0.top = 1
border0.bottom = 1
border0.left =1
border0.right = 1
其中边框的类型有:
# Text values for these borders attributes:
# left, right, top, bottom and diag
'no_line': 0x00,
'thin': 0x01,
'medium': 0x02,
'dashe