利用Python操作excel表格之xlwt介绍
by:授客 QQ:1033553122
直接上代码
案例1
#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'shouke'
import xlwt
if __name__ == '__main__':
work_book = xlwt.Workbook()
sheet1 = work_book.add_sheet('sheet1') # 添加表单 add_sheet(表单名称)
sheet1.write(0, 0, 'data_of_cell') # 往表单中写入数据(0,0 -> 第1行,第1列,注意:索引从0开始)
work_book.save('e:\test.xls') # 保存文件
运行结果:
案例2
#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'shouke'
import xlwt
if __name__ == '__main__':
work_book = xlwt.Workbook()
sheet1 = work_book.add_sheet('sheet1')
sheet1.write(0, 0, 'data_of_cell')
sheet1.write(0, 0, 'data_of_cell')
work_book.save('e:\test.xls')
运行结果:
解决方法:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'shouke'
import xlwt
if __name__ == '__main__':
work_book = xlwt.Workbook()
sheet2 = work_book.add_sheet('sheet2', cell_overwrite_ok=True) # 添加表单,允许覆盖单元格
sheet2.write(0, 0, 'data_of_cell')
sheet2.write(0, 0, 'data_of_cell')
work_book.save('e:\test.xls')
案例3
#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'shouke'
import xlwt
if __name__ == '__main__':
work_book = xlwt.Workbook()
sheet2 = work_book.add_sheet('sheet2', cell_overwrite_ok=True) # 添加表单 add_sheet(表单名称)
style = xlwt.XFStyle() # 初始化风格
# 格创建字体
font = xlwt.Font()
font.name = 'Times New Roman'
font.bold = True
style.font = font # 添加字体风格
# 基于上述风格,把数据写入表单
sheet2.write(0, 0, 'Times New Roman bold text', style)
work_book.save('e:\test.xls') # 保存文件
运行结果:
其它:参考源示例代码
参考链接:略