如何使用php更新docx文件的自定义属性
我想读取docx文件,并想更改word文档(*.docx)的某些部分。我已经把docx转换成zip了。我想在docx文件中添加新的自定义属性(docProps/custom.xml)。当我创建新的docx文件时。我可以通过php word添加自定义属性。但是,我想读取docx文件并更新自定义属性。使用phpword是不可能的 当我将docx转换为zip并打开docpProps/custom.xml时。默认情况下,它提供的xml内容如下所示: 当前xml内容:如何使用php更新docx文件的自定义属性,php,Php,我想读取docx文件,并想更改word文档(*.docx)的某些部分。我已经把docx转换成zip了。我想在docx文件中添加新的自定义属性(docProps/custom.xml)。当我创建新的docx文件时。我可以通过php word添加自定义属性。但是,我想读取docx文件并更新自定义属性。使用phpword是不可能的 当我将docx转换为zip并打开docpProps/custom.xml时。默认情况下,它提供的xml内容如下所示: 当前xml内容: <?xml version=&
<?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>
任何人都知道这是怎么可能的,请回复或给我示例脚本。我可以使用以下代码更新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();
}
$zip=new\ZipArchive;
//打开这个Zip文件
如果($zip->open('helloWorld.docx')==true){
//获取自定义xml内容
$xmlContent=$zip->getFromName('docProps/custom.xml');
//更新docPros/custom.xml内容
$updatedXmlContent='0
121
乱数假文
12
';
//用上面创建的新内容替换内容。
$zip->addFromString('docProps/custom.xml',$updatedXmlContent);
$zip->close();
}
文件只是XML。使用SimpleXML修改文件
您看过phpOffice/phpWord库了吗?是的,无法通过phpWord访问。您能澄清一下您想要添加哪些自定义属性吗?此外,到目前为止,您尝试了什么?正如目前所写的,这个问题需要更多的细节。@WOUNDEDStevenJones我在问题中添加了更多细节,但这些细节还不够。向我们展示您已经使用但失败的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();
}
$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();
}