【问题标题】:Extract text from doc and docx从 doc 和 docx 中提取文本
【发布时间】:2011-07-29 07:02:06
【问题描述】:

我想知道如何阅读 doc 或 docx 的内容。我使用的是 Linux VPS 和 PHP,但如果有使用其他语言的更简单的解决方案,请告诉我,只要它在 linux 网络服务器下工作即可。

【问题讨论】:

标签: php linux vps docx doc


【解决方案1】:

我建议,使用 apache Tika 提取文本,您可以提取多种类型的文件内容,例如 .doc/.docx 和 pdf 等等。

【讨论】:

    【解决方案2】:

    您可以使用 Apache Tika 作为它提供 REST API 的完整解决方案。

    另一个不错的库是RawText,因为它可以对图像进行 OCR,并从任何文档中提取文本。它不是免费的,并且可以通过 REST API 运行。

    使用 RawText 提取文件的示例代码:

    $result = $rawText->extract($your_file)
    

    【讨论】:

      【解决方案3】:

      Parse .docx, .odt, .doc and .rtf documents

      我编写了一个库,可以根据此处和其他地方的答案解析 docx、odt 和 rtf 文档。

      我对 .docx 和 .odt 解析所做的主要改进是该库处理描述文档的 XML 并尝试使其符合 HTML 标记,即 em标签。这意味着如果您将库用于 CMS,文本格式不会丢失

      你可以得到它here

      【讨论】:

      • 太棒了!使用它可以在使用 mPDF 创建的 PDF 中合并 .doc 和 .docx。
      • 这个类比其他库表现更好:)
      • 这个库也能获取图片吗?
      • @Akintunde-Rotimi 我可以帮你看看
      • 谢谢Luke..我也发到了github repo
      【解决方案4】:

      我在 doc 到 txt 的转换器功能中做了一些改进

      private function read_doc() {
          $line_array = array();
          $fileHandle = fopen( $this->filename, "r" );
          $line       = @fread( $fileHandle, filesize( $this->filename ) );
          $lines      = explode( chr( 0x0D ), $line );
          $outtext    = "";
          foreach ( $lines as $thisline ) {
              $pos = strpos( $thisline, chr( 0x00 ) );
              if (  $pos !== false )  {
      
              } else {
                  $line_array[] = preg_replace( "/[^a-zA-Z0-9\s\,\.\-\n\r\t@\/\_\(\)]/", "", $thisline );
      
              }
          }
      
          return implode("\n",$line_array);
      }
      

      现在它保存空行,txt 文件逐行查找。

      【讨论】:

        【解决方案5】:

        这只是一个 .DOCX 解决方案。对于 .DOC 或 .PDF,您需要为 PDF 使用类似 pdf2text.php 的其他内容

        function docx2text($filename) {
           return readZippedXML($filename, "word/document.xml");
         }
        
        function readZippedXML($archiveFile, $dataFile) {
        // Create new ZIP archive
        $zip = new ZipArchive;
        
        // Open received archive file
        if (true === $zip->open($archiveFile)) {
            // If done, search for the data file in the archive
            if (($index = $zip->locateName($dataFile)) !== false) {
                // If found, read it to the string
                $data = $zip->getFromIndex($index);
                // Close archive file
                $zip->close();
                // Load XML from a string
                // Skip errors and warnings
                $xml = new DOMDocument();
            $xml->loadXML($data, LIBXML_NOENT | LIBXML_XINCLUDE | LIBXML_NOERROR | LIBXML_NOWARNING);
                // Return data without XML formatting tags
                return strip_tags($xml->saveXML());
            }
            $zip->close();
        }
        
        // In case of failure return empty string
        return "";
        }
        
        echo docx2text("test.docx"); // Save this contents to file
        

        【讨论】:

        • id 不适用于 .doc 扩展名。它没有 word/document.xml 而是它有 _rels/.rels.xml 这种情况该怎么办??????
        • 你帮了我很多。我正在考虑如何在 PHP 中计算 docx 中的单词数。我只是没想到strip_tags
        • 这似乎不能正确处理回车。段落末尾的单词被合并到下一段的传入单词中。似乎它需要类似以下内容:$content = str_replace('</w:r></w:p></w:tc><w:tc>', " ", $content); $content = str_replace('</w:r></w:p>', "\r\n", $content); 由“M Khalid Junaid”回答提出
        • 我用过 $xml->formatOutput = true;在加载 xml 之前,它帮助我回车。
        【解决方案6】:

        我使用 docxtotxt 提取 docx 文件内容。我的代码如下:

        if($extention == "docx")
        {   
            $docxFilePath = "/var/www/vhosts/abc.com/httpdocs/writers/filename.docx";
            $content = shell_exec('/var/www/vhosts/abc.com/httpdocs/docx2txt/docx2txt.pl     
            '.escapeshellarg($docxFilePath) . ' -');
        }
        

        【讨论】:

          【解决方案7】:

          这里我添加了从 .doc,.docx 单词文件中获取文本的解决方案

          How to extract text from word file .doc,docx php

          对于.doc

          private function read_doc() {
              $fileHandle = fopen($this->filename, "r");
              $line = @fread($fileHandle, filesize($this->filename));   
              $lines = explode(chr(0x0D),$line);
              $outtext = "";
              foreach($lines as $thisline)
                {
                  $pos = strpos($thisline, chr(0x00));
                  if (($pos !== FALSE)||(strlen($thisline)==0))
                    {
                    } else {
                      $outtext .= $thisline." ";
                    }
                }
               $outtext = preg_replace("/[^a-zA-Z0-9\s\,\.\-\n\r\t@\/\_\(\)]/","",$outtext);
              return $outtext;
          }
          

          对于.docx

          private function read_docx(){
          
                  $striped_content = '';
                  $content = '';
          
                  $zip = zip_open($this->filename);
          
                  if (!$zip || is_numeric($zip)) return false;
          
                  while ($zip_entry = zip_read($zip)) {
          
                      if (zip_entry_open($zip, $zip_entry) == FALSE) continue;
          
                      if (zip_entry_name($zip_entry) != "word/document.xml") continue;
          
                      $content .= zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
          
                      zip_entry_close($zip_entry);
                  }// end while
          
                  zip_close($zip);
          
                  $content = str_replace('</w:r></w:p></w:tc><w:tc>', " ", $content);
                  $content = str_replace('</w:r></w:p>', "\r\n", $content);
                  $striped_content = strip_tags($content);
          
                  return $striped_content;
              }
          

          【讨论】:

          • 嗨,我用它来打开 doc 文件,但我只得到随机字符有什么想法我做错了什么吗?
          • 谢谢 .doc 文件工作正常。但是 .docx 文件不起作用。我使用了上面的代码。我的 .docx 文件的 Mime 类型显示为“应用程序/msword”。我有什么要补充的吗?
          【解决方案8】:

          我的解决方案是 Antiword 用于 .doc,docx2txt 用于 .docx

          假设你控制一个linux服务器,下载每一个,解压然后安装。我在系统范围内安装了每一个:

          反词:make global_install
          docx2txt:make install

          然后使用这些工具将文本提取到php中的字符串中:

          //for .doc
          $text = shell_exec('/usr/local/bin/antiword -w 0 ' . 
              escapeshellarg($docFilePath));
          
          //for .docx
          $text = shell_exec('/usr/local/bin/docx2txt.pl ' . 
              escapeshellarg($docxFilePath) . ' -');
          

          docx2txt 需要 perl

          no_freedom 的解决方案确实从 docx 文件中提取文本,但它可以删除空格。我测试的大多数文件都有一些实例,其中应该分隔的单词之间没有空格。当您想对正在处理的文档进行全文搜索时,效果不佳。

          【讨论】:

            【解决方案9】:

            试试ApachePOI。它适用于 Java。我想你在 Linux 上安装 Java 不会有任何困难。

            【讨论】:

              猜你喜欢
              • 2017-09-17
              • 1970-01-01
              • 2011-08-06
              • 2013-10-30
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多