我想读取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();
}这是怎么可能的,任何人都知道,请回复或给我示例脚本。
发布于 2020-08-28 04:39:42
我可以使用以下代码更新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();
}发布于 2020-08-28 04:58:02
这个文件就是XML。使用SimpleXML修改文件
https://stackoverflow.com/questions/63584137
复制相似问题