1

I have an Excel file and I am using Python to fill its rows and columns.

I want to use the following function to find the first empty row in the table and fill it:

from xlwt import Workbook, easyxf
def next_available_row(sheet):
    str_list = filter(None, sheet.col_values(1))  # error
    return str(len(str_list)+1)

wb=Workbook()
sheet=wb.add_sheet('sheet1')
sheet.write(0,0,'item')
sheet.write(0,1,'cost')
sheet.write(next_available_row(sheet),0,'potato')
sheet.write(next_available_row(sheet),1,4)

but I get the following error:

AttributeError: 'sheet' object has no attribute 'col_values'

What should I do?

handle
  • 5,859
  • 3
  • 54
  • 82
Mehdi
  • 1,146
  • 1
  • 14
  • 27
  • 1
    `What should I do?`- that's simple, post a [mcve], if you want me to have a chance to help you ;-) You don't even mention what library you are using... – handle Apr 10 '18 at 16:26
  • I edited my question. – Mehdi Apr 10 '18 at 16:32
  • 1
    Why don't you edit this into a single, short, piece of code that someone (I) can easily run on their computer to try and see where it's going wrong? (OK, but have you tested it? You're at least still missing `def`..) – handle Apr 10 '18 at 16:44
  • I edited again. – Mehdi Apr 10 '18 at 16:46
  • 1
    [The documentation](https://xlwt.readthedocs.io/en/latest/api.html#xlwt.Worksheet.Worksheet) does not say anything about `col_values`. Neither does [the source code](https://github.com/python-excel/xlwt/search?q=col_values). Where did you get this from? – handle Apr 10 '18 at 16:50
  • https://stackoverflow.com/questions/40781295/how-to-find-the-first-empty-row-of-a-google-spread-sheet-using-python-gspread – Mehdi Apr 10 '18 at 17:04
  • Thanks for mentioning it ... only now. You should read that again. They are using a different library, `gspread` (as you can see in the title, the tags, the code, ...). – handle Apr 10 '18 at 17:10

2 Answers2

1

The library you are using, xlwt, is for writing .xls spreadsheets only, and does not have the method col_values (to read its contents), as the error message already states (correctly).

The function next_available_row() (from How to find the first empty row of a google spread sheet using python GSPREAD?) that you want to use to search for an empty cell is based on a different library, gspread, and that is apparently not for Excel files (e.g. .xls, note there are several versions of this file type).

So you probably are looking for an entirely different library, one that reads and writes Excel files.

http://www.python-excel.org/ lists several libraries (including your xlrd):

Or maybe try to manage something by reading the file first, e.g. with xlwt's sister project, xlrd.

handle
  • 5,859
  • 3
  • 54
  • 82
0

Seems that has no col_values method on xlwt API. http://xlwt.readthedocs.io/en/latest/api.html

Maybe using together the xlrd you can reach your goal. http://xlrd.readthedocs.io/en/latest/api.html?highlight=col_values#xlrd-sheet

Marcello Mello
  • 191
  • 1
  • 3