MS Word文档文件生成索引

时间:2012-09-29 07:15:10

标签: php ms-word phpword

我使用phpword生成一个MS Word文档,有什么办法帮助我在生成的文件末尾生成一个索引?

我认为可行的一种方法是读取生成的MS Word文件并找到每个单词的位置(例如MS Word文件中的页码),然后在单独的MS Word文件中重新生成索引。

有没有更好的方法?
所需文件的示例:

A 
Animal 51,98 
Apple 11,54,99 

B
Basket 55  
...
..
etc

3 个答案:

答案 0 :(得分:1)

我想你可以阅读文档的标题:阅读这篇文章,不完全是你想要的,但也许修改可能是正确的方法:

Automatically generate nested table of contents based on heading tags

答案 1 :(得分:1)

我认为这会对你有帮助

See Here

答案 2 :(得分:1)

老实说,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 / 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:\ tidid元素都发生了变化,老实说,这些元素分散在各处,我不确定它们是如何工作/计算的,或者它们是否重要。

在〜/ word / _rels / document.xml.rels中,以下内容被添加为Relationships节点的子节点:

<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>

希望这会有所帮助。我知道这是一个复杂的解决方案,但我发现它是唯一可以帮助您实现自动化的方法。它会稍微超过the official, manual way of doing this,并且应该保留您所做的索引功能。我建议如果你认真这样做,使用WinMerge查看文件之间的差异(右键单击并使用比较特殊&gt; XML打开两个文件夹),祝你好运。如果它是一次性的,我只是咬紧牙关并手动完成。它可能更快,并且头痛更少。

相关问题