python操作excel文件有多种方式,这里主要介绍使用xlwt操作excel文件
一、介绍xlwt
使用xlwt可以操作与MS Excel 97/2000/XP/2003 XLS files兼容的excel文件。不兼容Excel2007/2010等 XLSX等格式的文件。
xlwt只能创建并保存新的文件,不能往原文件里追加内容。
如果要支持.xlsx格式需要使用xlsxwritter, 如果需要追加文件需要使用xlutils. 目前xlutils是2.0版,支持python3.5以下。
二、安装xlwt
pip install xlwt
三、主要 API介绍:
1、在未介绍xlwt主要api之前,先介绍几点注意事项:
1)如果使用editplus等编辑器编写python文件,保存文件时注意编码格式,要选用utf-8编码格式,否则在命令行执行时会报编码错误;
2)在python源文件中Tab制表符要用空格代替;
3)打开文件时,如果提示编码不对,需要使用二进制流的方式,比如 open('c: est.bmp','rb')
4)写入excel汉字乱码时,需要使用utf-8子附件,比如Workbook(encoding='utf-8')
5)将整数写入excel时,比如将array=([1,2,3])写入excel文件时,可能会报错,由于xlwt默认会转换成float类型,所以要显示声明类型,比如 data = numpy.arange(1,65,dtype=float).reshape((8,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,
'dashed': 0x03,
'dotted': 0x04,
'thick': 0x05,
'double': 0x06,
'hair': 0x07,
'medium_dashed': 0x08,
'thin_dash_dotted': 0x09,
'medium_dash_dotted': 0x0a,
'thin_dash_dot_dotted': 0x0b,
'medium_dash_dot_dotted': 0x0c,
'slanted_medium_dash_dotted': 0x0d,
2) Font(),
font0 = Font()
font0.name = '微软雅黑'
font0.struck_out = True
font0.bold = True
其中字体的属性有:
'bold': 布尔类型,
'charset': charset_map,
'color': 'colour_index',
'color_index': 'colour_index',
'colour': 'colour_index',
'colour_index': [colour_map, colour_index_func_15],
'escapement': {'none': 0, 'superscript': 1, 'subscript': 2},
'family': {'none': 0, 'roman': 1, 'swiss': 2, 'modern': 3, 'script': 4, 'decorative': 5, },
'height': IntULim(0xFFFF), # practical limits are much narrower e.g. 160 to 1440 (8pt to 72pt)
'italic': 布尔类型,
'name': any_str_func,
'outline': 布尔类型,
'shadow': 布尔类型,
'struck_out': 布尔类型,
'underline': [bool_map, {'none': 0, 'single': 1, 'single_acc': 0x21, 'double': 2, 'double_acc': 0x22, }],
其中:
charset_map = {
# Text values for font.charset
'ansi_latin': 0x00,
'sys_default': 0x01,
'symbol': 0x02,
'apple_roman': 0x4d,
'ansi_jap_shift_jis': 0x80,
'ansi_kor_hangul': 0x81,
'ansi_kor_johab': 0x82,
'ansi_chinese_gbk': 0x86,
'ansi_chinese_big5': 0x88,
'ansi_greek': 0xa1,
'ansi_turkish': 0xa2,
'ansi_vietnamese': 0xa3,
'ansi_hebrew': 0xb1,
'ansi_arabic': 0xb2,
'ansi_baltic': 0xba,
'ansi_cyrillic': 0xcc,
'ansi_thai': 0xde,
'ansi_latin_ii': 0xee,
'oem_latin_i': 0xff,
}
3) XFStyle()
style0 = XFStyle()
style0.font = font0
style0.borders = border0
4)设置单元格的宽度
wb = Workbook()
ws0 = wb.add_sheet('sheet0')
ws0.col(0).width = 100
5)格式化日期
style =XFStyle()
style.num_format_str = fmt
其中fmt类型有:
fmts = [
'M/D/YY',
'D-MM-YY',
'MM-YY',
'h:mm AM/PM',
'h:mm:ss AM/PM',
'h:mm',
'h:mm:ss',
'M/D/YY h:mm',
'mm:ss',
'[h]:mm:ss',
'mm:ss.0'
]
其他格式化
fmts = [
'general',
'0',
'0.00',
'#,##0',
'#,##0.00',
'"$"#,##0_);("$"#,##',
'"$"#,##0_);[Red]("$"#,##',
'"$"#,##0.00_);("$"#,##',
'"$"#,##0.00_);[Red]("$"#,##',
'0%',
'0.00%',
'0.00E+00',
'# ?/?',
'# ??/??',
'M/D/YY',
'D-MMM-YY',
'D-MMM',
'MMM-YY',
'h:mm AM/PM',
'h:mm:ss AM/PM',
'h:mm',
'h:mm:ss',
'M/D/YY h:mm',
'_(#,##0_);(#,##0)',
'_(#,##0_);[Red](#,##0)',
'_(#,##0.00_);(#,##0.00)',
'_(#,##0.00_);[Red](#,##0.00)',
'_("$"* #,##0_);_("$"* (#,##0);_("$"* "-"_);_(@_)',
'_(* #,##0_);_(* (#,##0);_(* "-"_);_(@_)',
'_("$"* #,##0.00_);_("$"* (#,##0.00);_("$"* "-"??_);_(@_)',
'_(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_)',
'mm:ss',
'[h]:mm:ss',
'mm:ss.0',
'##0.0E+0',
'@'
]
6)公式计算
wb =Workbook()
ws =wb.add_sheet('sheet0')
ws.write(0,0, Formula("SUM($A1:$C1)"))
3、插入图片
wb = Workbook()
ws =add_sheet('sheet0')
ws.insert_bitmap('test.bmp',0,0)
wb.save('d:ext.xls')
也可以将图片读入内存,然后写入excel文件
with open('test.bmp','rb') as bmpfile:
bmpdata = bmpfile.read()
ws.insert_bitmap_data(bmpdata,0,0)
4、工作簿保护,表格保护
wb = Workbook()
wb.protect = True
wb.wnd_protect = True
wb.obj_protect = True
ws0 = wb.add_sheet('sheet0')
ws0.protect = True
ws0.wnd_protect = True
ws0.obj_protect = True
ws0.scen_protect = True
ws0.password = "123456"
5、表格冻结
ws0.panes_frozen = True
ws0.horz_split_pos = 2
ws2.panes_frozen = True
ws2.vert_split_pos = 2
网友评论