Python错误:必须使用Workbook调用add_sheet()

时间:2018-12-03 19:15:45

标签: python xlrd xlwt

我遇到错误:

Traceback (most recent call last):
  File "clubranking.py", line 14, in <module>
    ws_out = wb_out.add_sheet(sheet_name)
TypeError: unbound method add_sheet() must be called with Workbook instance as first argument (got unicode instance instead)

尝试运行此python脚本时:

#import the writer
import xlwt
#import the reader
import xlrd
#open document
wb_in = xlrd.open_workbook('sussex.xlsx')
#get first sheet's name from the document
sheet_name = wb_in.sheet_names()[0]
#select sheet by name
ws_in = wb_in.sheet_by_name(sheet_name)
#init xlwt object, to be able to write data
wb_out = xlwt.Workbook
#initialise first sheet from the previously opened document, for     
writing
ws_out = wb_out.add_sheet(sheet_name)
#print the values in the second column of the first sheet
print first_sheet.col_values(1)
book = xlwt.Workbook('sussex.xlsx')
#in cell 0,0 (first cell of the first row) write "NIF"
first_sheet.write(0, 6, "NIF")
#in cell 0,0 (first cell of the first row) write "Points scored"
first_sheet.write(0, 6, "Points scored")

我不确定这个问题是什么,因为我是这个领域的初学者,但是任何帮助将不胜感激。我要实现的目标是我写的另一个问题:How to get Python script to write to existing sheet

1 个答案:

答案 0 :(得分:1)

您缺少在此处打开工作簿的信息。

wb_out = xlwt.Workbook

应类似于:

wb_out = xlwt.Workbook()

正确打开工作簿,问题应该得到解决。