Skip to content

python-docx Document Timezone Issue - Help Request #1542

@shengcruz

Description

@shengcruz

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:

  1. python-docx treats the passed datetime object as UTC time by default when storing
  2. Windows system converts UTC time to local timezone (UTC+8) when displaying file properties
  3. 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_time

Questions

  1. Is this a design bug or expected behavior in python-docx?
  2. Is there a more elegant solution (without manually subtracting 8 hours)?
  3. 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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions