首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用php更新docx文件的自定义属性

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

Stack Overflow用户
提问于 2020-08-26 01:27:15
回答 2查看 341关注 0票数 1

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

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

当前xml内容:

代码语言:javascript
复制
<?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文件中,如下所示

更新内容:

代码语言:javascript
复制
<?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代码:

代码语言:javascript
复制
        $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();
        }

这是怎么可能的,任何人都知道,请回复或给我示例脚本。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-08-28 04:39:42

我可以使用以下代码更新custom.xml:

代码语言:javascript
复制
    $zip = new \ZipArchive;

    // Open this Zip File
    if ($zip->open('helloWorld.docx') == true) {
        // Get custom xml content
        $xmlContent = $zip->getFromName('docProps/custom.xml');

        // Update docPros/custom.xml content
        $updatedXmlContent = '<?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="2" name="Id">
                <vt:lpwstr>121</vt:lpwstr>
            </property>
            <property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="3" name="Notes">
                <vt:lpwstr>Lorem ipsum</vt:lpwstr>
            </property>
            <property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="4" name="User">
                <vt:lpwstr>12</vt:lpwstr>
            </property>
        </Properties>';

        //Replace the content with the new content created above.
        $zip->addFromString('docProps/custom.xml', $updatedXmlContent);
        $zip->close();
    }
票数 2
EN

Stack Overflow用户

发布于 2020-08-28 04:58:02

这个文件就是XML。使用SimpleXML修改文件

https://www.php.net/manual/en/simplexml.examples-basic.php

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63584137

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档