无法写入excel AttributeError:'Worksheet'对象没有属性'write'

时间:2017-12-13 06:31:30

标签: python excel pandas openpyxl xlsxwriter

我正在尝试将文字写入excel我遵循此post。这工作得早,但现在不行。我明白了:

错误:

line 122, in <module>
    worksheet.write(0, 11, 'HI')
AttributeError: 'Worksheet' object has no attribute 'write'

DF1

A  E
c  d
c  D

代码:

writer = pd.ExcelWriter("C:\\33.xlsx")
df1.to_excel(writer, startrow=0, startcol=0, index = False)

worksheet = writer.sheets['Sheet1']
worksheet.write(0, 11, 'YO')
worksheet.write(1, 11, 'HI')

我也试过了:

import xlrd
import xlwt
from xlutils.copy import copy
import os.path
rb = xlrd.open_workbook('C:\\13.xlsx',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('C:\\13.xlsx')

我明白了:

    with open(filename, "rb") as f:
OSError: [Errno 22] Invalid argument: 'C:\\13.xlsx"'

如何修复AttributeError: 'Worksheet' object has no attribute 'write'

1 个答案:

答案 0 :(得分:3)

它给出的原因:AttributeError: 'Worksheet' object has no attribute 'write'

是因为我意识到我没有在这台电脑上安装xlsxwriter。

pip install xlsxwriter

现在可行。