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
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. - def convert_excel_time(t, hour24=True):
-
if t > 1:
-
t = t%1
-
seconds = round(t*86400)
-
minutes, seconds = divmod(seconds, 60)
-
hours, minutes = divmod(minutes, 60)
-
if hour24:
-
if hours > 12:
-
hours -= 12
-
return "%d:%d:%d PM" % (hours, minutes, seconds)
-
else:
-
return "%d:%d:%d AM" % (hours, minutes, seconds)
-
return "%d:%d:%d" % (hours, minutes, seconds)
-
-
print convert_excel_time(0.400983796)
-
print convert_excel_time(0.900983796, hour24=False)
-
print convert_excel_time(0.4006944444444)
-
print convert_excel_time(1.4006944444444)
Output: - >>> 9:37:25 AM
-
21:37:25
-
9:37:0 AM
-
9:37:0 AM
-
>>>
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?
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. - import datetime
-
import win32com.client
-
application = win32com.client.Dispatch("Excel.Application")
-
application.Visible=0
-
wb = application.Workbooks.Open("yourapp.xls")
-
t = wb.Worksheets("Sheet3").Range("A1").Value
-
wb.Worksheets("Sheet3").Range("B1").Value = str(datetime.timedelta(t))
-
wb.Close(SaveChanges=1)
-
application.Quit()
-
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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",...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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...
| |