使用 php 编辑 .doc 或 .docx 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3369061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Edit .doc or .docx file using php
提问by user343695
I have to modify the uploaded .doc or .docx file in php. I googled but i only found how to read that. I want the word file as it is and put text at the bottom of that MS Word file at run time. How is this possible anyone know please reply or give me example script.
我必须在 php 中修改上传的 .doc 或 .docx 文件。我用谷歌搜索,但我只找到了如何阅读。我想要原样的 word 文件,并在运行时将文本放在该 MS Word 文件的底部。这怎么可能有人知道请回复或给我示例脚本。
Thanks,
谢谢,
回答by Matthias Backhaus
I'm the developer of PHPWord. You can use the PHPWord_Template Class to open an existing DOCX File and then replace some text marks with your individual text.
我是 PHPWord 的开发者。您可以使用 PHPWord_Template 类打开现有的 DOCX 文件,然后用您的个人文本替换一些文本标记。
Alternatively you can open the DOCX file with the ZipArchive extension and then read/write every xml File (document.xml, header.xml, footer.xml, ...) you want. This method is nothing else than the PHPWord_Template class. ;)
或者,您可以打开带有 ZipArchive 扩展名的 DOCX 文件,然后读取/写入您想要的每个 xml 文件(document.xml、header.xml、footer.xml 等)。这个方法只不过是 PHPWord_Template 类。;)
回答by JON
I have same requirement for Edit .docor .docxfile using php and i have find solution for it. And i have write post on It :: http://www.onlinecode.org/update-docx-file-using-php/
我对使用 php编辑.doc或.docx文件有相同的要求,我已经找到了解决方案。我已经写了关于它的帖子 :: http://www.onlinecode.org/update-docx-file-using-php/
if($zip_val->open($full_path) == true)
{
// In the Open XML Wordprocessing format content is stored.
// In the document.xml file located in the word directory.
$key_file_name = 'word/document.xml';
$message = $zip_val->getFromName($key_file_name);
$timestamp = date('d-M-Y H:i:s');
// this data Replace the placeholders with actual values
$message = str_replace("client_full_name", "onlinecode org", $message);
$message = str_replace("client_email_address", "[email protected]", $message);
$message = str_replace("date_today", $timestamp, $message);
$message = str_replace("client_website", "www.onlinecode.org", $message);
$message = str_replace("client_mobile_number", "+1999999999", $message);
//Replace the content with the new content created above.
$zip_val->addFromString($key_file_name, $message);
$zip_val->close();
}
回答by David Yell
Try having a look at http://www.tinybutstrong.com/as it can create and edit Word documents, we use it in a mail merge style for generating invoices in doc and pdf.
尝试查看http://www.tinybutstrong.com/,因为它可以创建和编辑 Word 文档,我们以邮件合并样式使用它来生成 doc 和 pdf 格式的发票。

