0

I am trying to read date from excel file using xlrd module. Below is my code for this :

# Variables
myfile = '/home/mobaxterm/.git/Operation_Documentation/docs/Servicing Portal User &  Certificate Inventory.xlsx'
mydate = 'Expiration Date'
row_head = 0

# Import required modules
import xlrd
import datetime


today = datetime.date.today()
book = xlrd.open_workbook(myfile)
sheet = book.sheet_by_index(1)
for col_index in range(sheet.ncols):
        print xlrd.cellname(row_head,col_index),"-",
        print sheet.cell(row_head,col_index).value
        if sheet.cell(row_head,col_index).value == mydate:
                for raw_index in range(sheet.nrows):
                        expire = sheet.cell(raw_index,col_index).value
                        print expire
                        expire_date = datetime.datetime(*xlrd.xldate_as_tuple(expire, book.datemode))    
                        print 'datetime: %s' %  expire_date
                break

While running the code i am getting following error :

Traceback (most recent call last):
  File "cert_monitor.py", line 31, in <module>
    expire_date = datetime.datetime(*xlrd.xldate_as_tuple(expire, book.datemode))
  File "/usr/lib/python2.6/site-packages/xlrd/xldate.py", line 61, in xldate_as_tuple
    xldays = int(xldate)
ValueError: invalid literal for int() with base 10: 'Expiration Date'

Can anyone suggest what could be the issue here?

Thanks for your time.

6
  • 2
    Are you parsing the column header as an date? Commented Jun 19, 2013 at 15:29
  • Output types of arguments for xlrd.xldate_as_tuple: print type( expire ), print type( book.datemode ); then view a documentation for the function: probably one of the parameters has a wrong type. Commented Jun 19, 2013 at 15:32
  • Are you sure you are showing the code from line 31? mydate is the only name with the value 'Expiration Date' and the line that uses it does not look like it is the problem. You may need to show a simplified example of the file that shows the problem. Commented Jun 19, 2013 at 15:34
  • @dansalmo, this string is probably the header in the xls file. Commented Jun 19, 2013 at 15:36
  • Problematic part in the code is expire_date = datetime.datetime(*xlrd.xldate_as_tuple(expire, book.datemode)) print 'datetime: %s' % expire_date if i remove that part code is working till that point. So when i try to convert excel column data (float) to date format at that time it is throwing the error. Commented Jun 19, 2013 at 15:37

1 Answer 1

1

I believe that you should only skip the header:

for raw_index in range(1, sheet.nrows):
    ...

You are checking that sheet.cell(row_head,col_index).value == mydate, and then you want to iterate over the rows, but you should skip row_head first - it is ==mydate, which is not a date but a simple 'Expiration Date' string.

Sign up to request clarification or add additional context in comments.

5 Comments

Its date conversion of excel data which is causing problem. Other code is working absolutely fine.
@user2501825 have you tried my solution? your excel data you are trying to convert to date is not really a date. it is the column header.
Yeah after making that change i was able to print first date but still throws the same error and quits.python cert_monitor.py A1 - Server Name B1 - Task C1 - Username D1 - Purpose E1 - Password Expiration F1 - Expiration Date 41450.0 datetime: 2013-06-25 00:00:00 Traceback (most recent call last): File "cert_monitor.py", line 31, in <module> expire_date = datetime.datetime(*xlrd.xldate_as_tuple(expire, book.datemode)) File "/usr/lib/python2.6/site-packages/xlrd/xldate.py", line 61, in xldate_as_tuple xldays = int(xldate) ValueError: invalid literal for int() with base 10: ''
I think you have empty rows there.
You are awesome. After first raw two raws are empty. I guess i have to put logic to ignore empty raws. Thanks for pointing that out. Appreciate your time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.