# -*- coding: iso-8859-1 -*- #. // _ex_ReadDate.py #. // Lit une cellule de format date. from datetime import date from datetime import timedelta def ex_ReadDate( oSheet, nRow, nCol ): xDate = oSheet.Cells( nRow, nCol ).Formula #. // Date en nombre. try: dDate = date( 1899, 12, 30 ) + timedelta( days = int( xDate ) ) except Exception: dDate = None #. endtry if dDate is None: #. // Date au format "jj/mm/aaaa". try: dDate = date( int( xDate[ - 4 : ] ), int( xDate[ 3 : 5 ] ), int( xDate[ : 2 ] ) ) except Exception: dDate = None #. endtry #. endif #. // Date au format "aaaa-mm-jj". if dDate is None: try: dDate = date( int( xDate[ : 4 ] ), int( xDate[ 5 : 7 ] ), int( xDate[ - 2 : ] ) ) except Exception: dDate = None #. endtry #. endif return dDate ''' Dernière modification : 2022-02-13 Renvoie < None > si la lecture a échouée. [Todo] : Voir s'il y a des autres formats. [Exemple] import sys sys.path.append( "modules" ) from _ex_ReadDate import ex_ReadDate # // Lit une cellule de format date. from _ex_ExcelNew import ex_ExcelNew # // Ouvre Excel. oExcel = ex_ExcelNew() oBook = oExcel.Workbooks.Add() oSheet = oBook.Worksheets( 1 ) oSheet.Cells( 1, 1 ).Formula = "25/12/2021" oSheet.Cells( 1, 2 ).NumberFormat = "aaaa-mm-jj" #. // Excel met la date au format "jj/mm/aaaa" oSheet.Cells( 1, 2 ).Formula = "2021-12-25" oSheet.Cells( 1, 3 ).NumberFormat = "@" #. // Date au format texte. oSheet.Cells( 1, 3 ).Formula = "25/12/2021" dDate = ex_ReadDate( oSheet, 1, 1 ) print( '\nFormat "jj/mm/aaaa" :', dDate ) dDate = ex_ReadDate( oSheet, 1, 2 ) print( '\nFormat "aaaa-mm-jj" :', dDate ) dDate = ex_ReadDate( oSheet, 1, 3 ) print( '\n\nFormat texte "jj/mm/aaaa" :', dDate ) oExcel.DisplayAlerts = False oExcel = None [/Exemple] '''