docx文件基本结构

2021-04-21  本文已影响0人  antlove

template.docx


image.png

如上word模板 使用zip打开 如下


image.png

进入word


image.png
打开document.xml
<w:body>
    <w:tbl>
        <w:tr>
            <w:tc>
                <w:p>
                    <w:r>
                        <w:t>姓名</w:t>
                    </w:r>
                </w:p>
            </w:tc>
            <w:tc>
                <w:p>
                    <w:r>
                        <w:t>${name}</w:t>
                    </w:r>
                </w:p>
            </w:tc>
        </w:tr>
        <w:tr>
            <w:tc>
                <w:p>
                    <w:r>
                        <w:t>年龄</w:t>
                    </w:r>
                </w:p>
            </w:tc>
            <w:tc>
                <w:p>
                    <w:r>
                        <w:t>${age}</w:t>
                    </w:r>
                </w:p>
            </w:tc>
        </w:tr>
        <w:tr>
            <w:tc>
                <w:p>
                    <w:r>
                        <w:t>性别</w:t>
                    </w:r>
                </w:p>
            </w:tc>
            <w:tc>
                <w:p>
                    <w:r>
                        <w:t>${sex}</w:t>
                    </w:r>
                </w:p>
            </w:tc>
        </w:tr>
    </w:tbl>
    <w:p/>
    <w:p/>
    <w:p>
        <w:r>
            <w:drawing>
                <wp:inline distT="0" distB="0" distL="114300" distR="114300">
                    <wp:extent cx="4868545" cy="3017520"/>
                    <wp:effectExtent l="0" t="0" r="8255" b="11430"/>
                    <wp:docPr id="1" name="图片 1" descr="scene"/>
                    <wp:cNvGraphicFramePr>
                        <a:graphicFrameLocks
                                xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" noChangeAspect="1"/>
                    </wp:cNvGraphicFramePr>
                    <a:graphic
                            xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
                        <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
                            <pic:pic
                                    xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
                                <pic:nvPicPr>
                                    <pic:cNvPr id="1" name="图片 1" descr="scene"/>
                                    <pic:cNvPicPr>
                                        <a:picLocks noChangeAspect="1"/>
                                    </pic:cNvPicPr>
                                </pic:nvPicPr>
                                <pic:blipFill>
                                    <a:blip r:embed="rId4"/>
                                    <a:stretch>
                                        <a:fillRect/>
                                    </a:stretch>
                                </pic:blipFill>
                                <pic:spPr>
                                    <a:xfrm>
                                        <a:off x="0" y="0"/>
                                        <a:ext cx="4868545" cy="3017520"/>
                                    </a:xfrm>
                                    <a:prstGeom prst="rect">
                                        <a:avLst/>
                                    </a:prstGeom>
                                </pic:spPr>
                            </pic:pic>
                        </a:graphicData>
                    </a:graphic>
                </wp:inline>
            </w:drawing>
        </w:r>
    </w:p>
</w:body>

在如上xml中可以找到 ${name} ${age} ${sex} [以上xml为了方便阅读已做部分删减]
word中的替换文字就是通过这些占位符替换

<a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
    <pic:pic
            xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
        <pic:nvPicPr>
            <pic:cNvPr id="1" name="图片 1" descr="scene"/>
            <pic:cNvPicPr>
                <a:picLocks noChangeAspect="1"/>
            </pic:cNvPicPr>
        </pic:nvPicPr>
        <pic:blipFill>
            <a:blip r:embed="rId4"/>
            <a:stretch>
                <a:fillRect/>
            </a:stretch>
        </pic:blipFill>
        <pic:spPr>
            <a:xfrm>
                <a:off x="0" y="0"/>
                <a:ext cx="4868545" cy="3017520"/>
            </a:xfrm>
            <a:prstGeom prst="rect">
                <a:avLst/>
            </a:prstGeom>
        </pic:spPr>
    </pic:pic>
</a:graphicData>

在以上xml中找到 <a:blip r:embed="rId4"/>

image.png
打开_rels
image.png
打开document.xml.rels
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships
    xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
    <Relationship Id="rId6" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable" Target="fontTable.xml"/>
    <Relationship Id="rId5" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXml" Target="../customXml/item1.xml"/>
    <Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/image1.jpeg"/>
    <Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml"/>
    <Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings" Target="settings.xml"/>
    <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/>
</Relationships>

<Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/image1.jpeg"/>

image.png
打开 media文件
image.png
可以找到image1.jpeg
所以替换图片原理就是通过关联关系找到图片,然后在指定位置替换
上一篇下一篇

猜你喜欢

热点阅读