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

Docm support #195

Closed
brockfanning opened this issue Mar 26, 2021 · 4 comments · Fixed by #196
Closed

Docm support #195

brockfanning opened this issue Mar 26, 2021 · 4 comments · Fixed by #196

Comments

@brockfanning
Copy link
Contributor

Hello, I need to create Word docs from a template that is a docm (not docx) file. When I try with a particular docm file I get: Error: Could not find main document (e.g. document.xml) in [Content_Types].xml

If I use a docx file I don't get that problem, so I suspect that docm files are not supported. I was just wondering if anyone knew of a work-around for getting a docm file to work as a template. I will dive into the code and see what I can do, but also wanted to get feedback from the experts.

@jjhbw
Copy link
Collaborator

jjhbw commented Mar 26, 2021

Hi @brockfanning ,

Interesting, I've never used docm files before.

This library unzips the .docx file and tries to find the path to the document.xml file by reading [Content_Types].xml. I just checked, and the relevant line in [Content_Types].xml is slightly different in docm files.

.docx

<Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/>

.docm

<Override PartName="/word/document.xml" ContentType="application/vnd.ms-word.document.macroEnabled.main+xml"/>

So the first step of any attempt to extend this library with docm support would be to refactor the below function to work with docm-derived [Content_Types].xml files.

docx-templates/src/main.ts

Lines 409 to 424 in 10b6e16

export function getMainDoc(contentTypes: NonTextNode): string {
const MAIN_DOC_MIME = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml' as const;
for (const t of contentTypes._children) {
if (!t._fTextNode) {
if (t._attrs.ContentType === MAIN_DOC_MIME) {
const path = t._attrs.PartName;
if (path) {
return path.replace('/word/', '');
}
}
}
}
throw new TemplateParseError(
`Could not find main document (e.g. document.xml) in ${CONTENT_TYPES_PATH}`
);
}

That would be step 1, I think.

I don't know whether there are other major differences in structure between the two file formats, so there may be other more serious blockers before docm can be supported.

@brockfanning
Copy link
Contributor Author

Thanks @jjhbw - I gave that a try and it appears to work without any issues. The docm could be used as a template without errors, and the output file - so long as it is created with a docm extension - loads fine and the macros are intact. I just opened #196 in case it's helpful.

@jjhbw
Copy link
Collaborator

jjhbw commented Mar 27, 2021

@brockfanning thanks for contributing! Looks great. I've merged your PR and added a commit on top to please the typechecker (743a552) and another one with a test case (29af430).

@brockfanning
Copy link
Contributor Author

Great! 👍

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

Successfully merging a pull request may close this issue.

2 participants