Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding core properties does not work and corrupts file #101

Open
manuelbastuck opened this issue Jan 3, 2024 · 1 comment
Open

Adding core properties does not work and corrupts file #101

manuelbastuck opened this issue Jan 3, 2024 · 1 comment

Comments

@manuelbastuck
Copy link

I tried to add core properties (like category, version, ...) to a document with docxcompose. However, the resulting file, when openend with Microsoft Word, seemed to be "corrupted". Word complained about "unreadable content" and suggested to "recover" the file (wording might not be exact, translated from the German warning message). The recovery works and the file is displayed, however, the core properties are not present, neither before nor after.

After some digging, my tentative explanation is that the namespace prefix for custom properties should be "op" (according to here) instead of "cp" which is used in docxcompose. "cp" definitely overwrites the python-docx namespace prefix for "core properties" in utils.py. After I replaced every occurence of "cp" with "op" in utils.py and properties.py, everything seems to work as expected for me.

MWE:

from docx import Document
from docxcompose.composer import Composer

composer = Composer(Document())

# adding any of these (at least) result in a "corrupted" file
composer.doc.core_properties.version = "0"
# composer.doc.core_properties.keywords = "keyword"
# composer.doc.core_properties.category = "category"

composer.save("test.docx")
@MiquelBarceloG
Copy link

MiquelBarceloG commented Jun 18, 2024

Yes! I was getting crazy about this.

Funny enough, this generates valid docx files with all correct properties...

from docx import Document

doc=Document()

doc.core_properties.version = "0"
doc.core_properties.keywords = "keyword"
doc.core_properties.category = "category"

from docxcompose.composer import Composer

composer = Composer(doc)
composer.save("test.docx")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants