473,326 Members | 2,102 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Reading time field from excell sheet by python?

Hi,

How do i read time field from excell sheet by python.

I am getting "0.400694444444" value but sheet has "9:37:00 AM" .

How to convert it ?
Regards,
Ashok
Feb 13 '09 #1
3 4537
bvdet
2,851 Expert Mod 2GB
The excel value represents the time as a portion of one day starting at midnight. There are 86400 seconds in one day, 60 seconds in one minute, and 60 minutes in one hour.
Expand|Select|Wrap|Line Numbers
  1. def convert_excel_time(t, hour24=True):
  2.     if t > 1:
  3.         t = t%1
  4.     seconds = round(t*86400)
  5.     minutes, seconds = divmod(seconds, 60)
  6.     hours, minutes = divmod(minutes, 60)
  7.     if hour24:
  8.         if hours > 12:
  9.             hours -= 12
  10.             return "%d:%d:%d PM" % (hours, minutes, seconds)
  11.         else:
  12.             return "%d:%d:%d AM" % (hours, minutes, seconds)
  13.     return "%d:%d:%d" % (hours, minutes, seconds)
  14.  
  15. print convert_excel_time(0.400983796)
  16. print convert_excel_time(0.900983796, hour24=False)
  17. print convert_excel_time(0.4006944444444)
  18. print convert_excel_time(1.4006944444444)
Output:
Expand|Select|Wrap|Line Numbers
  1. >>> 9:37:25 AM
  2. 21:37:25
  3. 9:37:0 AM
  4. 9:37:0 AM
  5. >>> 
Feb 13 '09 #2
more simple solution

import datetime
print datetime.timedelta(0.400694444444)

Now i am faceing another problem,
I want to write the time into next column, but i am getting this below error.
self._cell_types[rowx][colx],
IndexError: array index out of range

Any solution?
Feb 17 '09 #3
bvdet
2,851 Expert Mod 2GB
@ashokd001
True, but datetime.timedelta returns hours:minutes:seconds only so you lose AM and PM.

@ashokd001
You have not posted your code, so I have no idea how you are reading from or writing to an Excel file. win32com.client.Dispatch() is the only way I am a bit familiar with.
Expand|Select|Wrap|Line Numbers
  1. import datetime
  2. import win32com.client
  3. application = win32com.client.Dispatch("Excel.Application")
  4. application.Visible=0
  5. wb = application.Workbooks.Open("yourapp.xls")
  6. t = wb.Worksheets("Sheet3").Range("A1").Value
  7. wb.Worksheets("Sheet3").Range("B1").Value = str(datetime.timedelta(t))
  8. wb.Close(SaveChanges=1)
  9. application.Quit()
  10.  
Feb 17 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Niyazi | last post by:
Hi, I have to retrieve a data from AS400 DB2 and after working with data I have to export into one of existing Excel file. I can connect into specific library in AS400 DB2 using AS400...
1
by: Mustafa | last post by:
I have an ASP script where i am generating the excell sheet dynamically i am passing some column header text which is long text so i want it to display it vertically in column (cell).In excell i...
2
by: nivas.meda | last post by:
Hi, I have an excel sheet with a graph and cells.If i change the value in the excel cells the graph will reflect.Now i am going to implement this functionality in html page.I successfully saved...
9
by: Hi5 | last post by:
Hi, I have designed a databasewhich is now able to store all data from my client's Excel sheet.Now I am looking for a good way to move their data into this Db. If I want to do this myself will...
8
by: Nick M | last post by:
Hello All, Excellent info here Thanks! I am very new to using access in general and I am on a learning curve. I'm trying to import an excel workbook (with worksheets) into an access db via a...
2
by: 675i76 | last post by:
I need to import a spreadsheet but the spread sheet has blank collums and the first row contains abbreviations (e.g. DEPT. - with period). the transferspreadsheet gives me field names "noname1",...
2
seshu
by: seshu | last post by:
hi every body This is seshu i have doubt in mysql i have some date in excell sheet to export that data i have copied all the data into a text file and the wrote this code in...
1
nirmalsingh
by: nirmalsingh | last post by:
hai all, i want to set width, alignment for excell sheet columns programatically through c#, how to do this? my coding public void exportToExcel(DataGridView myGrid, string...
1
Ali Rizwan
by: Ali Rizwan | last post by:
Hi all, I m creating a database. The data for database will fetched from an excell sheet. Now how can i read an excell sheet and update my database with that excell sheet. Or I want to show...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.