我想阅读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();
}
任何人怎么可能知道,请回复或给我示例脚本。
我可以使用以下代码更新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();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句