-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Open
Description
Issue Description
When using python-docx to generate Word documents and setting core_properties.created and core_properties.modified, the file timestamp shown in Windows Explorer is 8 hours ahead of the actual local time (Beijing Time, UTC+8).
Environment Information
- Operating System: Windows 10/11
- Python Version: Python 3.11
- python-docx Version: Latest
- Local Timezone: Beijing Time (UTC+8)
- System Time: 13:45 (correct)
- Word Document Displayed Time: 21:45 (incorrect, 8 hours ahead)
Reproduction Code
from docx import Document
from datetime import datetime
doc = Document()
# Set document metadata timestamps
core_props = doc.core_properties
core_props.created = datetime.now()
core_props.modified = datetime.now()
doc.save('test.docx')Problem Analysis
After multiple tests, I found:
python-docxtreats the passeddatetimeobject as UTC time by default when storing- Windows system converts UTC time to local timezone (UTC+8) when displaying file properties
- Result: System time 13:00 → docx stores as UTC 13:00 → Windows displays as 21:00 (UTC+8 conversion)
Current Workaround
from datetime import datetime, timedelta
# Pass "current time minus 8 hours" so Windows adds back 8 hours when displaying
correct_time = datetime.now() - timedelta(hours=8)
doc.core_properties.created = correct_time
doc.core_properties.modified = correct_timeQuestions
- Is this a design bug or expected behavior in
python-docx? - Is there a more elegant solution (without manually subtracting 8 hours)?
- Should I pass timezone-aware datetime objects? (Tested but didn't work)
Expectations
Hoping the official team can provide:
- Clear documentation on time handling
- Or direct support for local timezone time setting
Dear community experts, is this a known issue? Are there any better solutions?
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels