First and foremost: thanks for any help in advance. I'm a total programming novice, and my code will reflect that. I'll attempt to describe what I'm attempting to do, and display the code. Again, thank you for your time and explanations.
The objective: I'm wanting to have python open an existing excel file (output.xls), and input a value (in this case, 'test text') to the next available row in that document. I've attempted to use a 'while' loop, and an 'if' statement to get this done. Although neither return errors, both of them fail to correctly move the output past the second line. Here's what I've got.
from xlrd import open_workbook
from xlutils.copy import copy
import xlwt
wb = open_workbook('output.xls',formatting_info=True)
sheet = wb.sheet_by_name("sheet 1")
#This is the simple test text here.
a = 'just a test'
rb = copy(wb)
ws = rb.get_sheet(0)
row = 0
cell = sheet.cell(row,4)
What I'm trying to say below is -WHILE- the cell isn't blank (type 6), then add one to the row and repeat. IE: Keep going until you've hit a blank row in the fourth column.
while cell.ctype != 6:
row = row + 1
ws.write(row,4,a)
And here, I'm hoping to confirm the results.
print cell.ctype
rb.save('output.xls')
Regardless, when I run the code, it doesn't seem to get past the first line. It's as though the code says, "great, the first cell isn't type six," but doesn't get past that. Despite hours of searching on the net, I can't seem to find a reason as to why.
Any help / guidance is tremendously appreciated.
--- EDIT ---
Here's the errors I'm receiving to the suggested responses. The error is identical.
Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\Folder", line 19, in <module>
cell = sheet.cell(row,4)
File "C:\Python27\lib\site-packages\xlrd\sheet.py", line 395, in cell
xfx = self.cell_xf_index(rowx, colx)
File "C:\Python27\lib\site-packages\xlrd\sheet.py", line 421, in cell_xf_index
xfx = self._cell_xf_indexes[rowx][colx]
IndexError: list index out of range
You never move to the next cell. Just changing the variable row
doesn't influence cell
. Try this code instead:
cell = sheet.cell(row,4)
while cell.ctype != 6:
row = row + 1
if row >= sheet.nrows:
break
cell = sheet.cell(row,4) # <-- actually move the "pointer" in the excel sheet