@adi issues without a description are closed.
Will you add dotx support in future releases? Currently python-docx doesn't allowed it. However, simple dotx file amends are working fine after adding proper content types.
I expect we will. Just state what you are looking for in plain language so a reader can understand without having to guess.
@adi- you're getting me mentioned all over the place. :-)
I, too, would like to use this library with .dotx files, along with .docm files. Currently, when you try to open a dotx file, it throws an exception here. https://github.com/python-openxml/python-docx/blob/b36e40fbe2857112de9441ba69e18f07e5e152a4/docx/api.py#L28
I was able to monkey-patch support by adding the content types application/vnd.openxmlformats-officedocument.wordprocessingml.template.main+xml and application/vnd.ms-word.document.macroEnabled.main+xml to that file and to PartFactory.part_type_for in __init__.py.
Great, thanks @divergentdave, that's the detail we need :)
I'm trying to get a dotx converted to a docx with no modifications, for now.
After applying the monkey patches @divergentdave mentioned I hoped for the best and tried:
template_doc = Document(dotx_filepath)
template_doc.save(docx_filepath)
But when opening in the app then Word complains (with no useful error).
I realise this was naive, but I don know what the underlying technical differences really are and wondered if there is something that can be done with he python-docx API to do the conversion?
Thanks
Wayne
I would try changing the content type of the document part to match a regular Word document and see if that does the trick. As far as I know, there are no actual differences between a template and a regular document, just essentially the content type acting as a flag to let Word know to behave differently with that document.
The document part is available on Document.part.
Content types are enumerated in docx.opc.constants. I think the one you want is WML_DOCUMENT_MAIN. I believe this will change the content type:
from docx.opc.constants import CONTENT_TYPE as CT
document = Document('foo.dotx')
document_part = document.part
document_part._content_type = CT.WML_DOCUMENT_MAIN
Let us know how you go.
thank you @scanny , that worked, Word opens the doc with zero complaints.
@scanny I got a similar issue about a ValueError which shows me that content type is ‘application/vnd.openxmlformats-officedocument.themeManager+xml’. And I don't know how to fix this. Like adding a content type? Do I need to alter the source code?
@Chesh1re please post the stack trace and the (minimum) code to produce that error. There's something similar that happens when trying to open a pre-Word 2007 .doc file, which are not supported, so that might be worth a quick check. Also I think there was something like this that happened when opening a file that had a certain kind of Excel sheet embedded or something.
Anyway, we'll need the stack trace and triggering code to help you.
@scanny Thanks. I find the document file I am dealing with is a file converted from PDF file, so I guess they are not supported by python-docx. I want to get the tables from this .doc file, so there do have a certain kind of Excel sheet embedded which means you're right.
This kind of .doc files really drive me crazy. Maybe I need to find another way to get the job done. But thanks anyway.
Hi. I was looking for a way of _creating_ a DOTX file using python-docx. This Issue was the closest reference I found to my problem.
I would like to leave my solution here, in case somebody else has the same problem:
import docx
document = docx.Document()
document.add_paragraph('Helloworld.')
document_part = document.part
document_part._content_type = 'application/vnd.openxmlformats-officedocument.wordprocessingml.template.main+xml'
document.save('foo.dotx')
Most helpful comment
@adi- you're getting me mentioned all over the place. :-)