【发布时间】:2010-07-30 06:12:39
【问题描述】:
我必须在 php 中修改上传的 .doc 或 .docx 文件。我用谷歌搜索,但我只找到了如何阅读。我想要 word 文件原样,并在运行时将文本放在该 MS Word 文件的底部。这怎么可能有人知道请回复或给我示例脚本。
谢谢,
【问题讨论】:
标签: php
我必须在 php 中修改上传的 .doc 或 .docx 文件。我用谷歌搜索,但我只找到了如何阅读。我想要 word 文件原样,并在运行时将文本放在该 MS Word 文件的底部。这怎么可能有人知道请回复或给我示例脚本。
谢谢,
【问题讨论】:
标签: php
您只需替换预定义的变量。 在以下代码中
https://github.com/PHPOffice/PHPWord 我也使用以下代码在phpword中解决了这个问题
if(!file_exists('file/word.docx')){
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('demo.docx');
$templateProcessor->setValue('name', 'Akbarali');
$templateProcessor->setValue('time','13.02.2021');
$templateProcessor->setValue('month', 'January');
$templateProcessor->setValue('state','Uzbekistan');
$templateProcessor->saveAs('file/word.docx');
}
这将改变 demo.docx 文件中的文字。然后将新文件保存在 word.docx 文件夹中。 您可以在演示 word 文件中以 ${name} 的形式定义变量。 即 ${name}、${time}、${month} 和 ${state}
【讨论】:
我对使用 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", "ingo@onlinecode.org", $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();
}
【讨论】:
我是 PHPWord 的开发者。您可以使用 PHPWord_Template 类打开现有的 DOCX 文件,然后用您的个人文本替换一些文本标记。
或者,您可以打开带有 ZipArchive 扩展名的 DOCX 文件,然后读取/写入您想要的每个 xml 文件(document.xml、header.xml、footer.xml,...)。这个方法就是 PHPWord_Template 类。 ;)
【讨论】:
您可以使用PHPWord。
【讨论】:
尝试查看http://www.tinybutstrong.com/,因为它可以创建和编辑 Word 文档,我们以邮件合并样式使用它来生成 doc 和 pdf 格式的发票。
【讨论】: