- Notifications
You must be signed in to change notification settings - Fork1.3k
Open
Description
Issue Description
When usingpython-docx to generate Word documents and settingcore_properties.created andcore_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
fromdocximportDocumentfromdatetimeimportdatetimedoc=Document()# Set document metadata timestampscore_props=doc.core_propertiescore_props.created=datetime.now()core_props.modified=datetime.now()doc.save('test.docx')
Problem Analysis
After multiple tests, I found:
python-docxtreats the passeddatetimeobject asUTC 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
fromdatetimeimportdatetime,timedelta# Pass "current time minus 8 hours" so Windows adds back 8 hours when displayingcorrect_time=datetime.now()-timedelta(hours=8)doc.core_properties.created=correct_timedoc.core_properties.modified=correct_time
Questions
- 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?
Metadata
Metadata
Assignees
Labels
No labels