Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

python-docx Document Timezone Issue - Help Request #1542

Open
@shengcruz

Description

@shengcruz

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:

  1. python-docx treats the passeddatetime object asUTC 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

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

  1. Is this a design bug or expected behavior inpython-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


      [8]ページ先頭

      ©2009-2026 Movatter.jp