我使用 phpword 生成 MS Word 文档,有什么方法可以帮助我在生成的文件末尾生成索引?
我认为可行的一种方法是读取生成的 MS Word 文件并找到每个单词的位置(例如 MS Word 文件中的页码),然后在单独的 MS Word 文件中重新生成索引。
有没有更好的方法?
所需文件的示例:
A
Animal 51,98
Apple 11,54,99
B
Basket 55
...
..
etc
老实说,shox,我不认为你在这里有很多好的选择。我对此进行了一些研究,因为这对我来说也很有趣,但是除了您那里有关如何使之成为可能的帖子之外,我在 phpword 文档或论坛中找不到任何东西。在它的背面,您可以尝试解压缩 docx 包(它是一个 zip 文件)并直接在其中操作 XML 文件。我不知道这将如何进行……假设正确,这与在 Word 中手动完成没有什么不同。我尝试使用一些填充文本,并手动为文档中一个术语的索引执行“标记全部”。我能找到以下内容:
添加为 ~/[Content_Types].xml 中的第一个子项:
<Override PartName="/customXml/itemProps1.xml" ContentType="application/vnd.openxmlformats-officedocument.customXmlProperties+xml" />
创建文件夹 ~/CustomXml -
在 ~/CustomXml 文件夹中,创建 item1.xml :
<b:Sources SelectedStyle="\APA.XSL" StyleName="APA" xmlns:b="http://schemas.openxmlformats.org/officeDocument/2006/bibliography" xmlns="http://schemas.openxmlformats.org/officeDocument/2006/bibliography"></b:Sources>
在 ~/CustomXml 文件夹中,创建 itemProps1.xml :
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ds:datastoreItem ds:itemID="{3DC430FE-7F6E-49D7-9EFC-E4F37E42ABA0}" xmlns:ds="http://schemas.openxmlformats.org/officeDocument/2006/customXml">
<ds:schemaRefs>
<ds:schemaRef ds:uri="http://schemas.openxmlformats.org/officeDocument/2006/bibliography"/>
</ds:schemaRefs>
</ds:datastoreItem>
创建文件夹 ~/CustomXml/_rels :在 ~/CustomXml/_rels 文件夹中,创建 item1.xml.rels :
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXmlProps" Target="itemProps1.xml"/>
</Relationships>
~/docProps 文件夹已更改,但它无关紧要,因为 word 在每次打开/关闭时都会重新生成/更新这些值,并且它们与文件的内容没有任何关系。
基本上,这会留下 ~/word 文件夹。因此, ~/word/styles.xml 更改为在末尾添加索引样式,如下所示:
<w:style w:type="paragraph" w:styleId="Index1">
<w:name w:val="index 1"/>
<w:basedOn w:val="Normal"/>
<w:next w:val="Normal"/>
<w:autoRedefine/>
<w:uiPriority w:val="99"/>
<w:semiHidden/>
<w:unhideWhenUsed/>
<w:rsid w:val="00C52B35"/>
<w:pPr>
<w:spacing w:after="0" w:line="240" w:lineRule="auto"/>
<w:ind w:left="220" w:hanging="220"/>
</w:pPr>
</w:style>
~/word/settings.xml 中的 w:rsid 元素都发生了变化,老实说,这些元素分散在各处,我不确定它们是如何工作/计算的,或者它们是否太重要。
在 ~/word/_rels/document.xml.rels 中,添加了以下内容作为关系节点的子节点:
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXml" Target="../customXml/item1.xml"/>
最后,也许是最重要的,在 ~/word/document.xml 中,我的索引术语(“sit”)的每次出现都后跟以下元素:
<w:r w:rsidR="00C52B35">
<w:fldChar w:fldCharType="begin"/>
</w:r>
<w:r w:rsidR="00C52B35">
<w:instrText xml:space="preserve">XE "</w:instrText>
</w:r>
<w:r w:rsidR="00C52B35" w:rsidRPr="00C90937">
<w:instrText>sit</w:instrText>
</w:r>
<w:r w:rsidR="00C52B35">
<w:instrText xml:space="preserve">"</w:instrText>
</w:r>
<w:r w:rsidR="00C52B35">
<w:fldChar w:fldCharType="end"/>
</w:r>
最后还有一点我插入索引的地方:
<w:p w:rsidR="00C52B35" w:rsidRDefault="00C52B35" w:rsidP="00DE5AB4">
<w:pPr>
<w:rPr>
<w:b/>
<w:noProof/>
</w:rPr>
<w:sectPr w:rsidR="00C52B35" w:rsidSect="00C52B35">
<w:pgSz w:w="12240" w:h="15840"/>
<w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720" w:gutter="0"/>
<w:cols w:space="720"/>
<w:docGrid w:linePitch="360"/>
</w:sectPr>
</w:pPr>
<w:r>
<w:rPr>
<w:b/>
</w:rPr>
<w:fldChar w:fldCharType="begin"/>
</w:r>
<w:r>
<w:rPr>
<w:b/>
</w:rPr>
<w:instrText xml:space="preserve">INDEX \c "2" \z "1033"</w:instrText>
</w:r>
<w:r>
<w:rPr>
<w:b/>
</w:rPr>
<w:fldChar w:fldCharType="separate"/>
</w:r>
</w:p>
<w:p w:rsidR="00C52B35" w:rsidRDefault="00C52B35">
<w:pPr>
<w:rPr>
<w:noProof/>
</w:rPr>
</w:pPr>
<w:r>
<w:rPr>
<w:noProof/>
</w:rPr>
<w:lastRenderedPageBreak/>
<w:br w:type="page"/>
</w:r>
</w:p>
<w:p w:rsidR="00C52B35" w:rsidRDefault="00C52B35">
<w:pPr>
<w:pStyle w:val="Index1"/>
<w:tabs>
<w:tab w:val="right" w:leader="dot" w:pos="4310"/>
</w:tabs>
<w:rPr>
<w:noProof/>
</w:rPr>
</w:pPr>
<w:r>
<w:rPr>
<w:noProof/>
</w:rPr>
<w:lastRenderedPageBreak/>
<w:t>sit, 1, 2</w:t>
</w:r>
</w:p>
<w:p w:rsidR="00C52B35" w:rsidRDefault="00C52B35" w:rsidP="00DE5AB4">
<w:pPr>
<w:rPr>
<w:b/>
<w:noProof/>
</w:rPr>
<w:sectPr w:rsidR="00C52B35" w:rsidSect="00C52B35">
<w:type w:val="continuous"/>
<w:pgSz w:w="12240" w:h="15840"/>
<w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720" w:gutter="0"/>
<w:cols w:num="2" w:space="720"/>
<w:docGrid w:linePitch="360"/>
</w:sectPr>
</w:pPr>
</w:p>
<w:p w:rsidR="00371DB1" w:rsidRPr="00371DB1" w:rsidRDefault="00C52B35" w:rsidP="00DE5AB4">
<w:pPr>
<w:rPr>
<w:b/>
</w:rPr>
</w:pPr>
<w:r>
<w:rPr>
<w:b/>
</w:rPr>
<w:lastRenderedPageBreak/>
<w:fldChar w:fldCharType="end"/>
</w:r>
</w:p>
希望这会有所帮助。我知道这是一个复杂的解决方案,但这是我能找到的唯一可以帮助您并以自动化方式实际完成的方法。它比官方的手动方式略胜一筹,并且应该保留您以这种方式执行的索引的功能。如果您认真执行此操作,我建议您使用WinMerge查看文件之间的差异(打开两个文件夹后,右键单击并使用 Compare Special > XML),祝您好运。如果它是一次性的,我只会硬着头皮手动完成。它可能更快并且头痛更少。
我想你可以阅读文档的标题:阅读这篇文章,并不是你想要的,但也许通过修改可能是正确的方法: