0

I am getting an error:

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)

When trying to run this python script:

#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")

I'm not sure what the issue is as I'm a beginner in this realm but any help would be greatly appreciated. What I am trying to achieve is outlined is this other question that I wrote: How to get Python script to write to existing sheet

J4G
  • 211
  • 5
  • 14
  • `wb_out = xlwt.Workbook` does not initialise the object because you're missing `()` at the end. You're just binding the class to another name. As a side note, there is such a thing as over-commenting code and things like `#import the writer` are distracting for me. You have to assume some base understanding of the people reading the code. – roganjosh Dec 03 '18 at 19:19
  • @roganjosh thank you for clarifying, that's very helpful (i wasn't aware of the distraction in commenting as I find it useful for now). I have added '()' and have a new error: 'NameError: name 'first_sheet' is not defined' – J4G Dec 03 '18 at 19:22

1 Answers1

1

You are missing opening a workbook here.

wb_out = xlwt.Workbook

which should be like:

wb_out = xlwt.Workbook()

Open your workbook properly and problem should be fixed.

ak_app
  • 170
  • 10
  • thanks! that gets rid of the error but i now get a new one: 'NameError: name 'first_sheet' is not defined' do you know how i solve that? – J4G Dec 03 '18 at 19:24
  • Exactly. Do you see 'first_sheet' anywhere defined before it is used? – ak_app Dec 03 '18 at 19:29
  • Previously opened sheet is "sheet_name". Do you wish to use same? You can replace 'first_sheet' with 'ws_out' – ak_app Dec 03 '18 at 19:32
  • i suppose so! i just want to take the first sheet of the spreadsheet, which is titled 'Sheet 1' but it could be called anything as this script is going to be opening sheets that don't follow any standard template – J4G Dec 03 '18 at 19:35