【问题标题】:How to modify .doc or .docx file in php如何在 php 中修改 .doc 或 .docx 文件
【发布时间】:2010-07-21 09:36:04
【问题描述】:

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

谢谢,


我使用了以下代码:-

$w='Test';
$fp = fopen('c:/text.doc', 'a+');
fwrite($fp, $w);
fclose($fp);

它附加了字符串,但没有显示在 word 文档中。 当我将 doc 文件的扩展名更改为 xml 时,它会在末尾显示字符串。 为什么它不应该显示在 doc 文件中。

谢谢,

【问题讨论】:

    标签: php tabs


    【解决方案1】:

    对于 Docx 文件,您可以轻松地使用诸如 TbsZip 之类的 zip 阅读器来读取和编辑主要的 XML 子文件(Docx 文件实际上是 zip 存档文件)。对于DOC文件,因为是二进制文件,所以很难。

    下面是 TbsZip 的代码示例:

    <?php
    
    $x = "Hello World!";
    
    include_once('tbszip.php');
    
    $zip = new clsTbsZip();
    
    // Open the document
    $zip->Open('mydoc.docx');
    $content = $zip->FileRead('word/document.xml');
    $p = strpos($content, '</w:body>');
    if ($p===false) exit("Tag </w:body> not found in document.");
    
    // Add the text at the end
    $content = substr_replace($content, '<w:p><w:r><w:t>'.$x.'</w:t></w:r></w:p>', $p, 0);
    $zip->FileReplace('word/document.xml', $content, TBSZIP_STRING);
    
    // Save as a new file
    $zip->Flush(TBSZIP_FILE, 'new.docx');
    

    【讨论】:

      【解决方案2】:

      DOCX 格式的文档应该是 XML(压缩),所以你可以尝试解析和修改它们...

      有关格式的详细信息,请参阅here

      【讨论】:

      • 感谢您的回复,实际上我正在上传 .doc 文件并在最后在该 doc 文件中插入一些数据。然后将 .doc 文件的 url 插入数据库。然后,当我想要那个 .doc 文件时,我会下载它。如果我将它转换为 xml,它在 word 文档中的变化。
      • Word 文档是 XML。至少从 MS Word 2007 开始。甚至 2003 已经是某种 XML,但可能无效。
      【解决方案3】:

      好吧,我建议将文件保存为 XML。当您在 MS Word 中编写文档时,请转到另存为 -> 其他格式 -> 在此处选择 XML。

      您仍然可以在 MS Word 中打开文档,并且可以轻松地使用 PHP DOM 或 SimpleXML 编辑 XML。

      要在底部添加一些文本,您只需在 XML 正文中添加一个新的 w:p 元素:

      <w:p w:rsidR="00CF175F" w:rsidRDefault="00CF175F">
      <w:r>
      <w:t>New text to be added</w:t>
      </w:r>
      </w:p>
      

      W 是一个命名空间。对于 Word 2007+,它是:

      http://schemas.openxmlformats.org/wordprocessingml/2006/main
      

      在旧的 XML 格式中,有不同的命名空间,因此您必须查看 MS 网页才能找到正确的名称。

      【讨论】:

        【解决方案4】:

        我有同样的任务在 php 中编辑 .doc.docx 文件,我已经使用了这个代码。

        参考http://www.onlinecode.org/update-docx-file-using-php/

            $full_path = 'template.docx';
            //Copy the Template file to the Result Directory
            copy($template_file_name, $full_path);
        
            // add calss Zip Archive
            $zip_val = new ZipArchive;
        
            //Docx file is nothing but a zip file. Open this Zip File
            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();
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-10-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-10-06
          • 2015-11-06
          • 2016-07-10
          相关资源
          最近更新 更多