Reading time field from excell sheet by python?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ashokd001
    New Member
    • Aug 2008
    • 30

    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
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    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.
    Code:
    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:
    Code:
    >>> 9:37:25 AM
    21:37:25
    9:37:0 AM
    9:37:0 AM
    >>>

    Comment

    • ashokd001
      New Member
      • Aug 2008
      • 30

      #3
      more simple solution

      import datetime
      print datetime.timede lta(0.400694444 444)

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

      Any solution?

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Originally posted by ashokd001
        more simple solution

        import datetime
        print datetime.timede lta(0.400694444 444)
        True, but datetime.timede lta returns hours:minutes:s econds only so you lose AM and PM.

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

        Any solution?
        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.
        Code:
        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()

        Comment

        Working...