python 利用 xlwt 生成excle

来源:互联网 发布:html5游戏前景知乎 编辑:程序博客网 时间:2024/05/16 03:17
在写入Excel表格之前,你必须初始化workbook对象,然后添加一个workbook对象。比如:
import xlwtwbk = xlwt.Workbook()sheet = wbk.add_sheet('sheet 1')
这样表单就被创建了,写入数据也很简单:
# indexing is zero based, row then columnsheet.write(0,1,'test text')
之后,就可以保存文件(这里不需要想打开文件一样需要close文件):
wbk.save('test.xls')
worksheet对象,当你更改表单内容的时候,会有警告提示。
sheet.write(0,0,'test')sheet.write(0,0,'oops') # returns error:# Exception: Attempt to overwrite cell:# sheetname=u'sheet 1' rowx=0 colx=0
解决方式:使用
cell_overwrite_ok=True来创建worksheet:
sheet2 =  wbk.add_sheet('sheet 2', cell_overwrite_ok=True)sheet2.write(0,0,'some text')sheet2.write(0,0,'this should overwrite')

主要问题: xlwt产生的表格,字符串中文要用unicode 编码 否则在office 下无法打开
原创粉丝点击