MS word 文档文档结构和 COM 调用和 python

MS word document document stucturre and COM calls and python

我正在使用 comptypes 调用函数并创建 ms-word 文档。第一次写这样的程序,有些地方不太明白,我想做的是:

下面是 VBA 中的一些代码片段,使用 VBA 是因为使用 comptypes 的翻译几乎是直接的,并且网上有更多 VBA 的示例。

Set myRange = ActiveDocument.Range(Start:= ...., End:= ...) //start and end can be any thing 
ActiveDocument.Sections.Add Range:=myRange  //Section A
Set newRange = ActiveDocument.Range(Start:= ...., End:= ...) //start and end can be any thing 
newRange.Paragraphs.Add

我卡在了 select 段 a1 并设置了它的文本。我缺少的是 say get collection of paragraphs in section A.

的功能

下面的VBA,根据问题中的代码,说明获取一个Document对象,添加一个Section,获取那个[=的Paragraphs 12=],获取文档中任何给定 SectionParagraphs,从 Paragraphs 集合中获取第一个或任何 Paragraph

Set doc = ActiveDocument   //More efficient if the Document object will be used more than once
Set section1 = doc.Sections.Add(Range:=myRange)  //Section A | Type Word.Section
Set section1Paras = section1.Paragraphs  //Type Word.Paragraphs
//OR
Set sectionParas = doc.Sections(n).Paragraphs //where n = section index number
Set para = sectionParas.First //OR =sectionParas(n) where n= paragraph index number