Python AttributeError: 'Sheet' 对象没有属性 'write'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21246091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AttributeError: 'Sheet' object has no attribute 'write'
提问by Peter Lazarov
I am trying to write a string in a cell within an excel file. My code is
我正在尝试在 excel 文件的单元格中写入一个字符串。我的代码是
import xlwt
import xlrd
workbook = xlrd.open_workbook('my_workbook.xls')
worksheet = workbook.sheet_by_index(0)
worksheet.write(0,2,"string")
While I was looking for a solution I leardned that it could be becouse my xlwt library has an old version. However when I checkied it I got xlwt: 0.7.5. And I was once again left clueless. Any help is appreciated.
当我在寻找解决方案时,我了解到这可能是因为我的 xlwt 库有一个旧版本。但是,当我检查它时,我得到了 xlwt: 0.7.5。而我又一次无能为力了。任何帮助表示赞赏。
采纳答案by Peter Lazarov
After Looking into the problem I found a solution using the xlwtlibrary to write the data on a virtual workbook and the xlutilslibrary to save it and thus make the virtual workbook into an actual .xls file.
在调查问题后,我找到了一个解决方案,使用xlwt库将数据写入虚拟工作簿和xlutils库中以保存数据,从而使虚拟工作簿成为实际的 .xls 文件。
import xlrd
import xlwt
from xlutils.copy import copy
import os.path
rb = xlrd.open_workbook('my_workbook.xls',formatting_info=True)
r_sheet = rb.sheet_by_index(0)
wb = copy(rb)
sheet = wb.get_sheet(0)
sheet.write(5,2,"string")
wb.save('my_workbook.xls')

