如何使用 php 更新 docx 文件的自定义属性

我想要阅读 docx 文件并且想要更改 word 文档 (*.docx) 的部分内容。我已经将 docx 转换为 zip 了。我想在 docx 文件中添加新的自定义属性 (docProps/custom.xml)。当我创建新的 docx 文件时。我可以通过 php word 添加自定义属性。但是,我想读取 docx 文件并更新自定义属性。使用 phpword 是不可能的。


当我将 docx 转换为 zip 并打开 docpProps/custom.xml 时。默认情况下,它提供 xml 内容,如下所示:


当前的xml内容:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<Properties

    xmlns="http://schemas.openxmlformats.org/officeDocument/2006/custom-properties"

    xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">

    <property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="1" name="Property Id">

        <vt:lpwstr>121</vt:lpwstr>

    </property>

</Properties>

我想添加新属性并保存到 zip 文件中,如下所示


更新内容:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<Properties

    xmlns="http://schemas.openxmlformats.org/officeDocument/2006/custom-properties"

    xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">

    <property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="1" name="Property Id">

        <vt:lpwstr>121</vt:lpwstr>

    </property>

    <property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="2" name="Description">

        <vt:lpwstr>Lorem ipsum</vt:lpwstr>

    </property>

    <property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="3" name="User Id">

        <vt:lpwstr>12</vt:lpwstr>

    </property>

</Properties>

我的PHP代码:


        $zip = new \ZipArchive;


        // Open this Zip File

        if ($zip->open('helloWorld.docx') == true) {

            // Get custom xml content

            $xmlContent = $zip->getFromName('docProps/custom.xml');


            // I want to update docProps/custom.xml file


            $zip->close();

        }

这怎么可能有人知道请回复或给我示例脚本。


繁花如伊
浏览 107回答 2
2回答

慕斯王

我可以使用以下代码更新 custom.xml:&nbsp; &nbsp; $zip = new \ZipArchive;&nbsp; &nbsp; // Open this Zip File&nbsp; &nbsp; if ($zip->open('helloWorld.docx') == true) {&nbsp; &nbsp; &nbsp; &nbsp; // Get custom xml content&nbsp; &nbsp; &nbsp; &nbsp; $xmlContent = $zip->getFromName('docProps/custom.xml');&nbsp; &nbsp; &nbsp; &nbsp; // Update docPros/custom.xml content&nbsp; &nbsp; &nbsp; &nbsp; $updatedXmlContent = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>&nbsp; &nbsp; &nbsp; &nbsp; <Properties&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xmlns="http://schemas.openxmlformats.org/officeDocument/2006/custom-properties"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="2" name="Id">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <vt:lpwstr>121</vt:lpwstr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </property>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="3" name="Notes">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <vt:lpwstr>Lorem ipsum</vt:lpwstr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </property>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="4" name="User">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <vt:lpwstr>12</vt:lpwstr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </property>&nbsp; &nbsp; &nbsp; &nbsp; </Properties>';&nbsp; &nbsp; &nbsp; &nbsp; //Replace the content with the new content created above.&nbsp; &nbsp; &nbsp; &nbsp; $zip->addFromString('docProps/custom.xml', $updatedXmlContent);&nbsp; &nbsp; &nbsp; &nbsp; $zip->close();&nbsp; &nbsp; }

SMILET

该文件只是 XML。使用SimpleXML修改文件https://www.php.net/manual/en/simplexml.examples-basic.php
打开App,查看更多内容
随时随地看视频慕课网APP