2

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
X5748
  • 23
  • 4

2 Answers2

2

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
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • Thanks for the quick help. I've tried something to this effect, and here's the error I receive. Edit: Actually, I can't get it to format in here. I'll edit the original question above with the error I'm getting. – X5748 Jan 09 '14 at 09:45
  • The error means you iterated beyond the last row in the sheet with data. Stop when `row >= sheet.nrows`. – Aaron Digulla Jan 09 '14 at 10:08
  • Thanks for the explanation -- definitely helps clear things up. +1 knowledge over here. – X5748 Jan 09 '14 at 10:17
1

You advance row index - but you don't read a new cell, so your cell remains the same, and you enter an endless loop

while cell.ctype != 6:
    row = row + 1
    cell = sheet.cell(row,4)

should work

EDIT:

You are getting running out of rows - easily fixed

try:
    while cell.ctype != 6:
        row = row + 1
        cell = sheet.cell(row,4)
except IndexError:
    <do something>

Personally, I have a feeling that your run too fast - you are trying to get too deep without learning basics

volcano
  • 3,578
  • 21
  • 28
  • Thanks for the edit / fix! It's working like a charm. Likewise: I think you're right about moving too quick. I'm definitely a poor learner. I'm trying to stagger learning what I want, and learning from the basics, and from time to time these types of incidents happen. Regardless, thanks so much for the help. You've made my day. :o) – X5748 Jan 09 '14 at 10:07