English 中文(简体)
我想将.docx转换为.dotx
原标题:I want to convert .docx to .dotx

我在.docx 文件中填入了一些邮件合并字段, 现在我要我的脚本将保存的.docx 文件转换为.dotx 文件。 我正在使用 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 Usersmemailmergeproject	est-output  + str(i) + r .docx )
   #do conversion here

底部是我想转换的地方, 正如你可以看到的, 我写了一个文件, 它就坐在一个文件夹里

问题回答

< a href=" "https://stackoverflow.com/uss/18975067/sana"\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

Prerequisites

首先,我们需要确保安装正确的包件(python-docx ),方法是在您选择的控制台运行以下行:

Note This can be a bit tricky because the top of the script should include "import docx", BUT the python package docx is actually an older deprecated version of the updated python-docx. So before you run the script you will want to run the following using the latter (python-docx) and NOT the former (docx)!!

pip install python-docx

现在确保您想要转换为 Word 模板的 文件(即 文件) 与您的脚本位于同一目录中。 这样您就可以避免将原始文档的完整文件路径包含在脚本本身中 :

The code:

import docx

# replace "foo.docx" below with file name of document to be converted
document = docx.Document( foo.docx )
document_part = document.part
document_part._content_type =  application/vnd.openxmlformats-officedocument.wordprocessingml.template.main+xml 
document.save( bar.dotx )

我可以确认这在Python 3.12的作品, 就像我刚才测试的一样。

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 )




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签