-1

I'm trying to make my program do a specific task when the content of the first row in my Excel file (which contains dates such as 2016-01-20) matches tomorrow's date, assigned to variable "d".

Here is an excerpt of the code:

reader = openpyxl.load_workbook(sys.argv[1])
sheet = reader.active

for row in range(4, sheet.max_row + 1):
    i = date.today()
    print i
    d = date.today() + timedelta(days=1)

    if row[0] == d:
        sms_count = 0
        total_sms_count = 0
        #do some task

However, I'm getting the error:

Traceback (most recent call last):
  File "SMS_Sender.py", line 49, in <module>
    if row[0] == d:
TypeError: 'int' object has no attribute '__getitem__'
Geralds-MacBook-Pro:demos geraldkwok$ 

Can anyone tell me why I can't match the dates in my Excel file column to the variable "d" - and what I can do to fix it?

Thank you.

korakorakora
  • 197
  • 1
  • 2
  • 11

1 Answers1

1

Okay, sorry for the fact that all our comments are somewhat confusing. What we mean is the following:

for row in range(4, sheet.max_row + 1):
    # do something

says: for each integer in the range 4 until sheet.max_row+1, do something. You have to open the Excel file (using xlrd or pandas is possible) and read its lines into some list rows, for example. Then, you could do:

for row in rows:

and you will have a row that contains a sentence!

===================================================================== EDIT:

Example code:

# Open the sheet
reader = openpyxl.load_workbook(sys.argv[1])
sheet = reader.active

# Get the day of tomorrow
d = date.today() + timedelta(days=1)

# For each row, compare first cell and day of tomorrow
for rowNum in range(1, sheet.max_row+1):
    firstCell = sheet.cell(row=rowNum, column=1)
    if int(firstCell) == d:
        # Do some task

I didn't test it, but this should do it, I think.

Community
  • 1
  • 1
Nander Speerstra
  • 1,496
  • 6
  • 24
  • 29
  • Edited my original post to show my variables `reader` and `sheet`; should I replace `for row in range...` with `for row in rows`, or should I add something like `for row in reader/sheet` under `for row in range..`? – korakorakora Jan 20 '16 at 07:13