Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading date as a string not float from excel using python xlrd [duplicate]

Tags:

Possible Duplicate:
How do I read a date in Excel format in Python?

My date can be among any field in an excel file but when I read it using python xlrd its being read as a float. Is there a way to read all the excel cells as string?

I want to prepare a script to generate a file having all the values in excel file separated by a pipe but this date thing is creating problem.

like image 914
Kundan Kumar Avatar asked Dec 19 '12 23:12

Kundan Kumar


People also ask

How do I convert a date in Excel to a date in python?

xldate_as_datetime() function is used to convert excel date/time number to datetime. datetime object. Parameters: This function accepts two parameters that are illustrated below: xldate: This is the specified excel date that will converted into datetime.

How do I view XLSX files with XLRD?

Run the following command from the terminal to install the required version of xlrd. After completing the installation process, create a python file with the following script to read the sales. xlsx file using the xlrd module. open_workbook() function is used in the script open the xlsx file for reading.


1 Answers

Excel stores dates as floats. If you want to convert them xlrd has a function to help you with this: xldate_as_tuple

An exmple:

import datetime, xlrd book = xlrd.open_workbook("myfile.xls") sh = book.sheet_by_index(0) a1 = sh.cell_value(rowx=0, colx=0) a1_as_datetime = datetime.datetime(*xlrd.xldate_as_tuple(a1, book.datemode)) print 'datetime: %s' % a1_as_datetime 
like image 91
jojo Avatar answered Sep 17 '22 15:09

jojo