When writing Python macros in Excel using PyXLL, you might notice that writing a date string like "10/02/2026" ends up appearing in Excel as 2/10/2026.
Example:
from pyxll import xl_macro, XLCell
@xl_macro
def my_test():
XLCell.from_range("Sheet1!A1").value = "10/02/2026"Instead of showing 10 Feb 2026, Excel may display 2 Oct 2026.
Why This Happens
This is normal Excel behavior — not a PyXLL issue.
Whenever a string looks like a date, Excel automatically converts it into a real date using your system’s regional settings. The same thing happens in VBA.
How to Stop Excel from Converting the String
Option 1 (Easiest): Prefix with a Single Quote
XLCell.from_range("Sheet1!A1").value = "'10/02/2026"Excel will:
Store it as text
Hide the leading
'Not convert it to a date
Option 2: Set the Cell Format to Text First
Set the cell’s number format to "@" (Text) before assigning the value. This prevents Excel from interpreting the string as a date.
@xl_macro
def my_test():
cell = XLCell.from_range("Sheet1!A1")
cell.to_range().NumberFormat = "@"
cell.value = "10/02/2026"
Best Practice
If you want a real date, pass a Python
datetimeobject.If you want a literal string, use
'or format the cell as Text first.
This avoids unexpected date swapping when using PyXLL to import CSV files or write data from Python into Excel.