亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 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 關注
  • 235 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號