一.安装xlwt模块
1.pip3
install
xlwt
二.简单使用xlwt
1.import
xlwt
#导入模块
2.workbook = xlwt.Workbook(encoding=
'utf-8'
)
#创建workbook 对象
3.worksheet = workbook.add_sheet(
'sheet1'
)
#创建工作表sheet
4.worksheet.write(0, 0,
'hello'
)
#往表中写内容,第一各参数 行,第二个参数列,第三个参数内容
5.workbook.save(
'students.xls'
)
#保存表为students.xls
6.#在Java中使用poi操作excel 在创建workbook对象的时候就需要写好表名,创建workbook对象完成后
7.#这个表就已经存在,但是使用xlwt 需要在最后调用保存的时候为表命名,并且只有调入save方法后才会#创建表
三.为内容设置style
1.workbook
=
xlwt.Workbook(encoding
=
'utf-8'
)
2.worksheet
=
workbook.add_sheet(
'sheet1'
)
#设置字体样式
3.font
=
xlwt.Font()
#字体
4.font.name
=
'Time New Roman'
#加粗
5.font.bold
=
True
#下划线
6.font.underline
=
True
#斜体
7.font.italic
=
True
#创建style
8.style
=
xlwt.XFStyle()
9.style.font
=
font
#根据样式创建workbook
10.worksheet.write(
0
,
1
,
'world'
, style)
11.workbook.save(
'students.xls'
)
四.合并单元格
使用xlwt 合并单元格时不用像poi通过style来设置,直接创建和并单元格就可以,
1.workbook
=
xlwt.Workbook(encoding
=
'utf-8'
)
2.worksheet
=
workbook.add_sheet(
'sheet1'
)
#通过worksheet调用merge()创建合并单元格
#第一个和第二个参数单表行合并,第三个和第四个参数列合并,
#合并第0列到第2列的单元格
3.worksheet.write_merge(
0
,
0
,
0
,
2
,
'first merge'
)
#合并第1行第2行第一列的单元格
4.worksheet.write_merge(
0
,
1
,
0
,
0
,
'first merge'
)
5.workbook.save(
'students.xls'
)
如果需要了解具体调合并单元格规则就自己试着合并,查看合并效果.才能清晰明了
五.设置单元格的对齐方式
1.workbook
=
xlwt.Workbook(encoding
=
'utf-8'
)
2.worksheet
=
workbook.add_sheet(
'sheet1'
)
3.alignment
=
xlwt.Alignment()
# 水平居中
4.alignment.horz
=
xlwt.Alignment.HORZ_CENTER
# 垂直居中
5.alignment.vert
=
xlwt.Alignment.VERT_CENTER
6.style
=
xlwt.XFStyle()
7.style.alignment
=
alignment
#设置单元格宽度
8.worksheet.col(
0
).width
=
6666
#设置单元格的高度
9.worksheet.row(
0
).height_mismatch
=
True
10.worksheet.row(
0
).height
=
1000
11.worksheet.write(
0
,
0
,
'hello world'
, style)
12.workbook.save(
'center.xls'
)
六.设置单元格调边框
1.workbook
=
xlwt.Workbook(encoding
=
'utf-8'
)
2.worksheet
=
workbook.add_sheet(
'sheet1'
)
3.border
=
xlwt.Borders()
# DASHED虚线
# NO_LINE没有
# THIN实线
4.border.left
=
xlwt.Borders.THIN
# 设置颜色
5.border.left_coloure
=
0x40b
6.border.right
=
xlwt.Borders.THIN
7.border.right_colour
=
0x40b
8.border.top
=
xlwt.Borders.THIN
9.border.top_colour
=
0x40b
10.border.bottom
=
xlwt.Borders.THIN
11.border.bottom_colour
=
0x40b
12.style
=
xlwt.XFStyle()
13.style.borders
=
border
14.worksheet.write(
0
,
0
,
'love'
, style)
15.workbook.save(
'dashed.xls'
)
七.设置单元格边框
1.workbook
=
xlwt.Workbook(encoding
=
'utf-8'
)
2.worksheet
=
workbook.add_sheet(
'sheet1'
)
3.border
=
xlwt.Borders()
# DASHED虚线
# NO_LINE没有
# THIN实线
4.border.left
=
xlwt.Borders.THIN
5.border.right
=
xlwt.Borders.THIN
6.border.top
=
xlwt.Borders.THIN
7.border.bottom
=
xlwt.Borders.THIN
8.style
=
xlwt.XFStyle()
9.style.borders
=
border
10.worksheet.write(
1
,
1
,
'love'
, style)
11.workbook.save(
'dashed.xls'
)
八.设置单元格背景色
1.workbook
=
xlwt.Workbook(encoding
=
'utf-8'
)
2.worksheet
=
workbook.add_sheet(
'sheet1'
)
3.pattern
=
xlwt.Pattern()
4.pattern.pattern
=
xlwt.Pattern.SOLID_PATTERN
# 8 through 63
# 0 = Black, 1 = White,
# 2 = Red, 3 = Green, 4 = Blue,
# 5 = Yellow, 6 = Magenta, 7 = Cyan,
# 16 = Maroon, 17 = Dark Green,
# 18 = Dark Blue, 19 = Dark Yellow ,
# almost brown), 20 = Dark Magenta,
# 21 = Teal, 22 = Light Gray,
# 23 = Dark Gray, the list goes on...
5.pattern.pattern_fore_colour
=
3
6.style
=
xlwt.XFStyle()
7.style.pattern
=
pattern
8.worksheet.write(
1
,
1
,
'shit'
, style)
9.workbook.save(
'shit.xls'
)
九.设置字体颜色
1.workbook
=
xlwt.Workbook(encoding
=
'utf-8'
)
2.worksheet
=
workbook.add_sheet(
'sheet1'
)
3.font
=
xlwt.Font()
# 设置字体为红色
4.font.colour_index
=
xlwt.Style.colour_map[
'red'
]
5.style
=
xlwt.XFStyle()
6.style.font
=
font
7.worksheet.write(
0
,
1
,
'world'
, style)
8.workbook.save(
'students.xls'
)
遇到的问题:
1.PermissionError: [Errno 13] Permission denied: 'dashed.xls'
原因:测试写入调时候文件被打开了,无法写入,关闭文件后再写入就ok了
2.TypeError: 'module' object is not callable
创建style的时候调错对象将
1.style
=
xlwt.Style()
改为
1.style
=
xlwt.XFStyle()