Last modified: Nov 19, 2025 By Alexander Williams
Handle Excel Dates and Times with Python xlrd
Working with dates in Excel files can be tricky. Python xlrd makes it easier. This guide shows you how.
Excel stores dates as serial numbers. This format needs conversion. We will cover the complete process.
Understanding Excel Date Storage
Excel does not store dates as text. It uses serial numbers instead. January 1, 1900 is number 1.
Each day adds one to the count. Times are decimal fractions. 0.5 represents 12:00 PM.
This system allows calculations. But it requires conversion for reading. Python xlrd provides tools for this.
Setting Up xlrd
First, install xlrd. Use pip for installation. Our guide on install xlrd and xlwt in Python covers this.
Basic setup is simple. Import the library after installation. Then you can load Excel files.
import xlrd
# Open the workbook
workbook = xlrd.open_workbook('sample_data.xls')
Loading Excel Files with xlrd
Start by loading your Excel file. Use the open_workbook function. This creates a workbook object.
Access sheets by index or name. Then read cells containing dates. Check our Python xlrd guide for details.
# Get the first sheet
sheet = workbook.sheet_by_index(0)
# Read a cell value
cell_value = sheet.cell_value(0, 0)
print(f"Raw cell value: {cell_value}")
Raw cell value: 44562.75
Identifying Date Cells
Not all numbers are dates. Check the cell type first. Use cell_type method.
Date cells have type 3. This indicates they are dates. Then you can convert them properly.
# Check cell type
cell_type = sheet.cell_type(0, 0)
print(f"Cell type: {cell_type}")
# Type 3 means date
if cell_type == 3:
print("This is a date cell")
Cell type: 3
This is a date cell
Converting Excel Dates to Python
Use xldate_as_tuple for conversion. This function converts serial numbers. It returns a tuple with date components.
The tuple contains year, month, day, and more. You can then create Python datetime objects. This makes dates usable.
# Convert Excel date to tuple
date_tuple = xlrd.xldate_as_tuple(cell_value, workbook.datemode)
print(f"Date tuple: {date_tuple}")
# Convert to datetime object
from datetime import datetime
python_date = datetime(*date_tuple)
print(f"Python datetime: {python_date}")
Date tuple: (2022, 1, 15, 18, 0, 0)
Python datetime: 2022-01-15 18:00:00
Understanding Datemode
Datemode is crucial for accuracy. Excel has two date systems. Windows uses 1900-based system.
Macintosh used 1904-based system. xlrd detects this automatically. Always use workbook.datemode.
Using wrong datemode gives incorrect dates. This is a common mistake. Our Python xlrd tutorial explains more.
Handling Time Values
Time values work similarly. They are decimal fractions of days. Convert them using the same method.
The tuple includes hour, minute, and second. You can extract time components. Or use the full datetime.
# Time conversion example
time_value = 0.75 # 6:00 PM in Excel
time_tuple = xlrd.xldate_as_tuple(time_value, workbook.datemode)
print(f"Time tuple: {time_tuple}")
time_only = datetime(1900, 1, 1, time_tuple[3], time_tuple[4], time_tuple[5])
print(f"Time only: {time_only.time()}")
Time tuple: (1900, 1, 1, 18, 0, 0)
Time only: 18:00:00
Working with Mixed Data
Real Excel files have mixed content. Some cells are dates, others are not. You need to check each cell.
Loop through rows and columns. Identify date cells. Convert them appropriately.
# Process multiple cells
for row in range(sheet.nrows):
for col in range(sheet.ncols):
cell_value = sheet.cell_value(row, col)
cell_type = sheet.cell_type(row, col)
if cell_type == 3: # Date cell
date_tuple = xlrd.xldate_as_tuple(cell_value, workbook.datemode)
python_date = datetime(*date_tuple)
print(f"Row {row}, Col {col}: {python_date}")
else:
print(f"Row {row}, Col {col}: {cell_value}")
Common Date Handling Issues
Several issues can occur. Leap year bug in Excel is known. February 29, 1900 doesn't exist.
Excel incorrectly includes it. xlrd handles this automatically. But be aware of this quirk.
Timezone issues may arise. Excel dates are timezone-naive. Keep this in mind for accuracy.
Best Practices
Always check cell types. Don't assume all numbers are dates. Verify before conversion.
Use the workbook's datemode. Don't hardcode this value. It varies between files.
Handle exceptions properly. Conversion may fail. Use try-except blocks for safety.
# Safe date conversion
def safe_date_conversion(cell_value, cell_type, datemode):
if cell_type == 3:
try:
date_tuple = xlrd.xldate_as_tuple(cell_value, datemode)
return datetime(*date_tuple)
except:
return None
return cell_value
Advanced Date Operations
You can perform date calculations. Convert to Python datetime first. Then use datetime operations.
Calculate differences between dates. Add or subtract time periods. Filter data by date ranges.
Check our guide on filter search Excel data for filtering techniques.
Conclusion
Handling Excel dates in Python is straightforward with xlrd. Remember to check cell types first. Always use the correct datemode.
Convert serial numbers to Python datetime objects. This enables proper date manipulation. Your data processing will be more accurate.
Practice with different Excel files. You will master date handling quickly. Happy coding with Python and Excel!