'I want to convert .docx to .dotx

I have populated some mail merge fields in a .docx file and now I want my script to convert the saved .docx file to a .dotx file. I am using Python 3.6.

from __future__ import print_function
from mailmerge import MailMerge
from datetime import date
from docx import Document
from docx.opc.constants import CONTENT_TYPE as CT
import csv
import sys
import os
import numpy as np
import pandas as pd

# . . .

for i in range(0, numTemplates):
   theTemplateName = templateNameCol[i]
   theTemplateFileLocation = templateFileLocationCol[i]
   template = theTemplateFileLocation
   document = MailMerge(template)
   print(document.get_merge_fields())

   theOffice = officeCol[i]
   theAddress = addressCol[i]
   theSuite = suiteCol[i]
   theCity = cityCol[i]
   theState = stateCol[i]
   theZip = zipCol[i]
   thePhoneNum = phoneNumCol[i]
   theFaxNum = faxNumCol[i]

   document.merge(
       Address = theAddress 
   )

   document.write(r'\Users\me\mailmergeproject\test-output' + str(i) + r'.docx')
   #do conversion here

Here at the bottom is where I want to do the conversion. As you can see, I've written a file and it's just sitting in a folder right now



Solution 1:[1]

Here is the code snippet for converting the .docx file to dotx. You have to change the content-type while saving the document

pip install python-docx

import docx

document = docx.Document('foo.dotx')
document_part = document.part
document_part._content_type = 'application/vnd.openxmlformats- 
                              officedocument.wordprocessingml.template.main+xml'
document.save('bar.docx')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1