为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
繁花如伊 2023-11-03 15:53:04
我想要阅读 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();        }这怎么可能有人知道请回复或给我示例脚本。
查看完整描述

2 回答

?
慕斯王

TA贡献1864条经验 获得超2个赞

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


    $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();

    }


查看完整回答
反对 回复 2023-11-03
?
SMILET

TA贡献1796条经验 获得超4个赞

该文件只是 XML。使用SimpleXML修改文件

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


查看完整回答
反对 回复 2023-11-03
  • 2 回答
  • 0 关注
  • 101 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信