【问题标题】:Edit .doc or .docx file using php使用 php 编辑 .doc 或 .docx 文件
【发布时间】:2010-07-30 06:12:39
【问题描述】:

我必须在 php 中修改上传的 .doc 或 .docx 文件。我用谷歌搜索,但我只找到了如何阅读。我想要 word 文件原样,并在运行时将文本放在该 MS Word 文件的底部。这怎么可能有人知道请回复或给我示例脚本。

谢谢,

【问题讨论】:

    标签: php


    【解决方案1】:

    您只需替换预定义的变量。 在以下代码中

    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}

    【讨论】:

      【解决方案2】:

      我对使用 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();
      }
      

      【讨论】:

        【解决方案3】:

        我是 PHPWord 的开发者。您可以使用 PHPWord_Template 类打开现有的 DOCX 文件,然后用您的个人文本替换一些文本标记。

        或者,您可以打开带有 ZipArchive 扩展名的 DOCX 文件,然后读取/写入您想要的每个 xml 文件(document.xml、header.xml、footer.xml,...)。这个方法就是 PHPWord_Template 类。 ;)

        【讨论】:

        • 我可以使用 PHPWord 在浏览器中打开文档进行编辑吗?
        • @UmairAyub 你的意思是像谷歌文档?没有人,PHPWord 仅用于以编程方式生成 word 文档。基于浏览器的文字处理器很重要,您不会只是找到一个可以为您处理所有这些的库。
        【解决方案4】:

        您可以使用PHPWord

        【讨论】:

        • 嗨,我已经使用了 PHPWORD,但它只创建 .docx 文件而不是修改。我想编辑现有的 word 文件。如果文件在 .doc 中,那么它将与 .doc 扩展名一样,如果文件是 .docx,那么它将采用相同的格式。请回复,谢谢,
        • 在我下载的 PHPWord 代码中,有一个在 .docx 模板中替换变量(格式为 ${varname} )并保存为新文件的示例。但是,如果您的文档中没有 varname 标签,则可能无法回答此问题。我认为 PHPWord 也不能处理 .doc 文件 - 可能需要 COM 类,或者附加到任何给定文件 (php.net/manual/en/book.com.php)
        【解决方案5】:

        尝试查看http://www.tinybutstrong.com/,因为它可以创建和编辑 Word 文档,我们以邮件合并样式使用它来生成 doc 和 pdf 格式的发票。

        【讨论】:

        • 感谢回复。但我在 tinybutstrong.com 中找不到我的要求。你能给我举个例子吗?谢谢,
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-06
        相关资源
        最近更新 更多