You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
extension: support .docm (macro-enabled) Word files #284
This module is great, but it doesn't currently work with docm files or macros.
Add in a way to both import macros into the VBProject and save docm files.
Currently, the only way to do this is pywin32 and if you both import a macro and save as docm for 5 files, it takes about 30 sec-1 minute which is obviously unacceptable.
Your module is perfectly positioned to take care of this problem and I'd hope to see an improvement sometime in the future.
Do you have an example of a .docm file you can share? (You can just drag and drop it in the comment box in response.)
I had understood that macros were stored in a binary format, so I'm not sure how you'd be able to generate them in Python, even if you could "import" them.
But I'll take a look at the file structure and see what we see.
The relationships one would be straightforward, and the data one as well I suppose, assuming there's a schema or other documentation out there, or someone willing to do the reverse engineering to determine what goes in there.
But I don't know how we'd do the binary bit. I would expect it's proprietary and undocumented, but haven't really looked into it.
Do you have any idea of what functionality might be useful and how it might be implemented?
I'm still doing research into this, but thought I'd share what I have so far. It appears the format is documented (how well, I have yet to determine) and some Python tools exist to parse it already.
The functionality I'd be looking for specifically would be to pass a string that contains the raw VBA source code and have a function that inserts it into a Word document. I've long been wanting a way to programmatically generate VBA source code and insert it into a document. Personally, this is useful to me if I want to generate a bunch of documents that are similar and only differ with say a single variable in the macro code.
From time to time the only way to discover how the MS API for Word (or PowerPoint for python-pptx) behaves is to experiment. And this is a bit of a pain given what it takes to do that when you're not running natively on Windows. We often need to do this while designing a new API element.
So it would be awesome to be able to take some plain-text code and be able to produce a test docx that runs that code once opened in Windows.
Based on what you sent, I'm thinking what we would need in python-docx (and could probably have in python-pptx as well), is a way to take an arbitrary "black-box" VBA binary and lodge it in the right place in the package, perhaps adding the right relationships and possibly the vbaData.xml. The VBA black-box could be written with one of the tools you pointed toward.
Let us know as you come up with more in your research. If we could get some kind of filthy hack or even hand-assembled file that proves the concept we'll definitely be interested in designing API support for this. We'd probably need a contributor to do the needful, so you might consider if you're willing to play that role for this one, let me know if you are :)
Hi Guys, just a request for some input if you will.
Adding support so you can open and save the .docm format (but not access or manipulate the macro-related parts) would be a relatively modest undertaking.
How much benefit do you see it that?
The likelihood that would be implemented in the "sooner" time frame is a lot higher I expect.
Reacted by jfcorbett, Simon Espigolé, Brett Ables, Majd Alsado and Tilman Bender
Reacted by jfcorbett, Ben Stähli, Andrew Boldi and Majd Alsado
Yes, this would be helpful to me. My macro is a one size fits all, so as long as I have a template file(which I do), opening a docm and saving it with macro contents intact would work perfectly well in my case. And there is also the requirement that it be editable like any docx file.
I am wondering if there is any progress/decision made on this topic. I rally need this extension for the project I am working on. We have some template files and they have some macros in them, if we save them as docx then we lose the macros and they are no longer useful to us. It would be very helpful to modify the document (without modifying the macros) as scanny describes. I found this link #212 where it is hinted that it might be possible to add this feature to a local copy of python-docx but I am not sure how I can implement this. Any help will be greatly appreciated. Thanks in advance.
@scanny I would benefit greatly of opening, changing and writing .docm files, without touching any macro code (as I understand it, writing or changing macros is the hard part...). you removed the shortlist label - are there any problems? Is I understood, @ozgurpolat already made opening possible? What is still missing, to have this feature released?
Hi all. An update on this issue would be greatly apprecciated. I would also be able to sponsor opening/editing/closing withoug touching VBA part...if this is still a modest undertaking? @scanny@mustard007@oezgan@ozgurpolat anyone?
It has been almost two years, i really had to scratch my head to remember this thread here. Sorry no update from me. I think I had converted the macro enabled document into a non macro word document since i was not interested in the macros. Beyond that i am afraid i cant help.
Activity
scanny commentedon Mar 22, 2016
Do you have an example of a .docm file you can share? (You can just drag and drop it in the comment box in response.)
I had understood that macros were stored in a binary format, so I'm not sure how you'd be able to generate them in Python, even if you could "import" them.
But I'll take a look at the file structure and see what we see.
ethack commentedon Mar 22, 2016
I would like to see this implemented as well. I've attached a sample document containing a Hello World macro.
Since Github wouldn't let me upload the document as-is, I changed the extension to ".zip".
MacroTest.docm.zip
For reference, here is the macro I put in the document:
scanny commentedon Mar 22, 2016
Thanks @ethack, just the ticket :)
Here's what the innards look like:
$ unzip -l MacroTest.docm Archive: MacroTest.docm Length Date Time Name -------- ---- ---- ---- 2485 01-01-80 00:00 [Content_Types].xml 590 01-01-80 00:00 _rels/.rels 1976 01-01-80 00:00 word/_rels/document.xml.rels 1977 01-01-80 00:00 word/document.xml 1295 01-01-80 00:00 word/header3.xml 1295 01-01-80 00:00 word/footer2.xml 1295 01-01-80 00:00 word/footer1.xml 1295 01-01-80 00:00 word/header2.xml 1295 01-01-80 00:00 word/header1.xml 1675 01-01-80 00:00 word/endnotes.xml 1681 01-01-80 00:00 word/footnotes.xml 1295 01-01-80 00:00 word/footer3.xml 6795 01-01-80 00:00 word/theme/theme1.xml 277 01-01-80 00:00 word/_rels/vbaProject.bin.rels 9728 01-01-80 00:00 word/vbaProject.bin 2699 01-01-80 00:00 word/settings.xml 1367 01-01-80 00:00 word/vbaData.xml 497 01-01-80 00:00 word/webSettings.xml 29856 01-01-80 00:00 word/styles.xml 712 01-01-80 00:00 docProps/app.xml 727 01-01-80 00:00 docProps/core.xml 1261 01-01-80 00:00 word/fontTable.xml -------- ------- 72073 22 filesNote the item word/vbaProject.bin. I've had a quick look, and no surprise, but it's definitely not plain text.
The other two files of interest are vbaProject.bin.rels:
... and vbaData.xml:
The relationships one would be straightforward, and the data one as well I suppose, assuming there's a schema or other documentation out there, or someone willing to do the reverse engineering to determine what goes in there.
But I don't know how we'd do the binary bit. I would expect it's proprietary and undocumented, but haven't really looked into it.
Do you have any idea of what functionality might be useful and how it might be implemented?
ethack commentedon Mar 23, 2016
I'm still doing research into this, but thought I'd share what I have so far. It appears the format is documented (how well, I have yet to determine) and some Python tools exist to parse it already.
https://msdn.microsoft.com/en-us/library/cc313094%28v=office.12%29.aspx
http://www.decalage.info/vba_tools
The functionality I'd be looking for specifically would be to pass a string that contains the raw VBA source code and have a function that inserts it into a Word document. I've long been wanting a way to programmatically generate VBA source code and insert it into a document. Personally, this is useful to me if I want to generate a bunch of documents that are similar and only differ with say a single variable in the macro code.
scanny commentedon Mar 24, 2016
This is very interesting @ethack :)
From time to time the only way to discover how the MS API for Word (or PowerPoint for python-pptx) behaves is to experiment. And this is a bit of a pain given what it takes to do that when you're not running natively on Windows. We often need to do this while designing a new API element.
So it would be awesome to be able to take some plain-text code and be able to produce a test docx that runs that code once opened in Windows.
Based on what you sent, I'm thinking what we would need in python-docx (and could probably have in python-pptx as well), is a way to take an arbitrary "black-box" VBA binary and lodge it in the right place in the package, perhaps adding the right relationships and possibly the vbaData.xml. The VBA black-box could be written with one of the tools you pointed toward.
Let us know as you come up with more in your research. If we could get some kind of filthy hack or even hand-assembled file that proves the concept we'll definitely be interested in designing API support for this. We'd probably need a contributor to do the needful, so you might consider if you're willing to play that role for this one, let me know if you are :)
[-]Add docm support[/-][+]extension: support .docm (macro-enabled) Word files[/+]scanny commentedon Apr 22, 2016
Hi Guys, just a request for some input if you will.
Adding support so you can open and save the .docm format (but not access or manipulate the macro-related parts) would be a relatively modest undertaking.
How much benefit do you see it that?
The likelihood that would be implemented in the "sooner" time frame is a lot higher I expect.
ethack commentedon Apr 22, 2016
Personally, I don't have a need for that use case.
scanny commentedon Apr 22, 2016
ok, thanks @ethack :)
JosephHardy91 commentedon Apr 22, 2016
Yes, this would be helpful to me. My macro is a one size fits all, so as long as I have a template file(which I do), opening a docm and saving it with macro contents intact would work perfectly well in my case. And there is also the requirement that it be editable like any docx file.
Thanks
scanny commentedon Apr 22, 2016
okay, good to know, thanks @blindidiot91 :)
I'll look to scope this one out.
mustard007 commentedon Dec 22, 2016
Hi!
I'm interested in that too.
Thanks!
ozgurpolat commentedon Mar 10, 2017
Hi,
I am wondering if there is any progress/decision made on this topic. I rally need this extension for the project I am working on. We have some template files and they have some macros in them, if we save them as docx then we lose the macros and they are no longer useful to us. It would be very helpful to modify the document (without modifying the macros) as scanny describes. I found this link #212 where it is hinted that it might be possible to add this feature to a local copy of python-docx but I am not sure how I can implement this. Any help will be greatly appreciated. Thanks in advance.
scanny commentedon Mar 13, 2017
Where do you run into a problem? Be specific and include actual error messages with trace back if any.
5 remaining items
scanny commentedon Apr 4, 2017
Glad you got it working @ozgurpolat :)
oezgan commentedon Aug 17, 2017
@ozgurpolat & @scanny
Thank you very much 👍
mustard007 commentedon Aug 18, 2017
Thanks both of you !
So now, we can work with .docm files ?
cucrisis commentedon Dec 10, 2017
Thanks awesome work
Update README.rst
snbby commentedon Apr 2, 2018
Hi,
According to the comments, this issue seems to be resolved.
But here is still an error, when I am trying to open .docm file:
raises
ValueError: file 'macro_file.docm' is not a Word file, content type is 'application/vnd.ms-word.document.macroEnabled.main+xml'Have I missed smth?
Or .docm support was not implemented?
benzkji commentedon Apr 4, 2019
@scanny I would benefit greatly of opening, changing and writing .docm files, without touching any macro code (as I understand it, writing or changing macros is the hard part...). you removed the shortlist label - are there any problems? Is I understood, @ozgurpolat already made opening possible? What is still missing, to have this feature released?
benzkji commentedon May 14, 2019
Hi all. An update on this issue would be greatly apprecciated. I would also be able to sponsor opening/editing/closing withoug touching VBA part...if this is still a modest undertaking? @scanny @mustard007 @oezgan @ozgurpolat anyone?
oezgan commentedon May 14, 2019
It has been almost two years, i really had to scratch my head to remember this thread here. Sorry no update from me. I think I had converted the macro enabled document into a non macro word document since i was not interested in the macros. Beyond that i am afraid i cant help.
benzkji commentedon Jul 4, 2019
@scanny I'll try to prepare a PR, including @ozgurpolat s approach.
benzkji commentedon Jul 4, 2019
but if you got no time, I'll let it be, and not fill up the PR list even more :|
benzkji commentedon Jul 4, 2019
(as I'll find a solution this or that way ;-) thanks for the work, though, great library!
feat: open and save .docm, .dotx and .dotm files