【问题标题】:Why does PHPword setvalue not replacing text in word Doc?为什么 PHPword setvalue 不替换 word Doc 中的文本?
【发布时间】:2021-10-09 08:44:35
【问题描述】:

我正在尝试使用 php (PHPword library) 编辑 word 文档。但是,我不确定为什么它不起作用。脚本没有抛出任何错误。

我的代码:

    <?php 
    require 'vendor/autoload.php';
    
    // $phpword = new \PhpOffice\PhpWord\PhpWord();
    $templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('test.docx');
    // $templateProcessor->setValues(array('firstname' => 'John', 'lastname' => 'Doe'));
    $templateProcessor->setValue('firstname', 'John');
    $templateProcessor->setValue('lastname', 'Doe');
    
    // var_dump($templateProcessor->setValues(array('firstname' => 'John', 'lastname' => 'Doe')));
    var_dump($templateProcessor->setValue('lastname', 'Doe'))
    ?>

我的话文档: word doc

输出: Output

我不知道为什么它不起作用。你能帮我么 ?谢谢

【问题讨论】:

    标签: php phpword


    【解决方案1】:

    setValue() 方法将设置值但不将其返回给 var_dump 。

    您可以添加:

    $pathToSave = 'path/to/save/file.ext';
    $templateProcessor->saveAs($pathToSave);
    

    并检查生成文档中的输出。

    【讨论】:

    • 谢谢。它现在正在工作。抱歉,我无法为您的答案投票,因为我需要 15 声望才能投票。
    • 很高兴它现在可以工作了。可以保存投票以备后用。 ;) 祝你工作顺利!
    猜你喜欢
    • 2018-01-06
    • 2013-05-23
    • 1970-01-01
    • 2014-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-12
    • 1970-01-01
    相关资源
    最近更新 更多