字词Python-创建目录 [英] Word & Python - Create Table of Contents

查看:92
本文介绍了字词Python-创建目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将pywin32.client扩展用于python并构建Word文档.我尝试了很多很好的方法来生成ToC,但都失败了.

I'm using the pywin32.client extension for python and building a Word document. I have tried a pretty good host of methods to generate a ToC but all have failed.

我认为我想做的是调用ActiveDocument对象,并通过MSDN页面中的以下示例创建一个对象:

I think what I want to do is call the ActiveDocument object and create one with something like this example from the MSDN page:

Set myRange = ActiveDocument.Range(Start:=0, End:=0) 
ActiveDocument.TablesOfContents.Add Range:=myRange, _ 
 UseFields:=False, UseHeadingStyles:=True, _ 
 LowerHeadingLevel:=3, _ 
 UpperHeadingLevel:=1

除Python外,类似:

Except in Python it would be something like:

wordObject.ActiveDocument.TableOfContents.Add(Range=???,UseFiles=False, UseHeadingStyles=True, LowerHeadingLevel=3, UpperHeadingLevel=1)

到目前为止,我已经使用选择"对象(下面的示例)构建了所有内容,并希望在第一页分页符之后添加此ToC.

I've built everything so far using the 'Selection' object (example below) and wish to add this ToC after the first page break.

以下是文档外观的示例:

Here's a sample of what the document looks like:

objWord = win32com.client.Dispatch("Word.Application")
objDoc = objWord.Documents.Open('pathtotemplate.docx') #
objSel = objWord.Selection
#These seem to work but I don't know why...
objWord.ActiveDocument.Sections(1).Footers(1).PageNumbers.Add(1,True)
objWord.ActiveDocument.Sections(1).Footers(1).PageNumbers.NumberStyle = 57
objSel.Style = objWord.ActiveDocument.Styles("Heading 1")
objSel.TypeText("TITLE PAGE AND STUFF")
objSel.InsertParagraph()
objSel.TypeText("Some data or another"
objSel.TypeParagraph()
objWord.Selection.InsertBreak()
####INSERT TOC HERE####

任何帮助将不胜感激!在理想情况下,我将使用Word GUI中可用的默认第一个选项,但这似乎指向文件并且难以访问(关于模板).

Any help would be greatly appreciated! In a perfect world I'd use the default first option which is available from the Word GUI but that seems to point to a file and be harder to access (something about templates).

谢谢

推荐答案

手动,在Word中编辑模板,添加ToC(最初为空)的任何介绍性内容,页眉/页脚等,然后在所需的位置您插入的文本内容(即在ToC之后)将放置一个唯一命名的书签.然后在您的代码中,基于模板创建一个新文档(或打开模板,然后将其保存为其他名称),搜索书签并在其中插入您的内容.保存到其他文件名.

Manually, edit your template in Word, add the ToC (which will be empty initially) any intro stuff, header/footers etc., then at where you want your text content inserted (i.e. after the ToC) put a uniquely named bookmark. Then in your code, create a new document based on the template (or open the template then save it to a different name), search for the bookmark and insert your content there. Save to a different filename.

这种方法具有各种优点-您可以在Word中格式化模板,而不必编写所有代码详细信息,因此,当有人说他们希望Normal字体更大时,您可以非常轻松地编辑模板以更新样式/smaller/pink,您只需编辑模板即可.确保在代码中使用样式,并且仅在格式与默认样式明显不同时才应用格式.

This approach has all sorts of advantages - you can format your template in Word rather than by writing all the code details, and so you can very easily edit your template to update styles when someone says they want the Normal font to be bigger/smaller/pink you can do it just by editing the template. Make sure to use styles in your code and only apply formatting when it is specifically different from the default style.

不确定您如何确定实际生成的ToC,可能会在每次保存时自动更新.

Not sure how you make sure the ToC is actually generated, might be automatically updated on every save.

这篇关于字词Python-创建目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆