• PHP将上传word文件,转化为Html格式,(多种转换方式)


    方法一: 通过PHPOffice(推荐)

      1:    composer require phpoffice/phpword    
      ```
                                            /*  通过composer安装 PHPOffice 
                                                需要
                                               或者用中国镜像:https://www.phpcomposer.com/
                                            */   
      ```
      2:  安装成功可看到 vendor文件夹
    
    
      3: 使用方法 :
      ```
      <?php 
          require 'vendor/autoload.php';
          $phpWord = PhpOfficePhpWordIOFactory::load('./4.docx');
          $xmlWriter = PhpOfficePhpWordIOFactory::createWriter($phpWord, "HTML");
          $xmlWriter ->save('./ceshi.htm');
      ``` 
    
    
        
         4:官方  
         /*
           官方案例:https://phpword.readthedocs.io/en/latest/general.html
           github: https://github.com/PHPOffice/PhpSpreadsheet
          */
    

    方法二:通过python

            ```
            from win32com import client as wc
            import sys
    
            def saveHtm():
          # print(wordPath)
          # print(htmPath)
          wordPath='E:/1.doc'
          htmPath='E:/1.htm'
          word = wc.Dispatch('Word.Application')
          print(word) 
          doc = word.Documents.Open('E:/1.doc')
          doc.SaveAs("E:/1.htm", 8)               //转化为htm格式 
          doc.SvaeAs("E:/1.fpt",17)                
    
          doc.Close() 
          word.Quit()
    
            if __name__ == '__main__':
          saveHtm()
            ```
    

    方法三:同时Offic API直接在网页显示word文档

      ```
      <iframe
     src="http://view.officeapps.live.com/op/view.aspx?src=公网上能访问的word文档地址" ></iframe>
    
      例
      src="http://view.officeapps.live.com/op/view.aspxsrc=newteach.pbworks.com%2Ff%2Fele%2Bnewsletter.docx"
    

    方法四:通过com组件

    需要所在宿主机,有offic的环境,Linxu下不能使用
     
    (不推荐)
    
    <?php
    /**
     * @desc   xmsb_wordToPdf         将word转换为pdf
     * @param  string    $wordPath    word原文件路径
     * @param  string    $outPath     pdf输出路径
     * @return string
     */
    function xmsb_wordToPdf($wordPath, $outPath)
    {
        // 原文件不存在则返回错误
        if(!file_exists($wordPath)) 
        {
            return 'word原文件不存在';
        }
        
        // 输出目录不存在则创建目录
        if(!file_exists($tmpPath = rtrim($outPath, basename($outPath))))
        {
            mkdir($tmpPath, 0777, true);
        }
        
        $filenamedoc = dirname(__FILE__)."/".$wordPath;
        $filenamepdf = dirname(__FILE__)."/".$outPath;
        
        // 删除已有同名文件
        if(file_exists($filenamepdf)) 
        {
            unlink($filenamepdf);
        }
        
        // 执行转换操作
        $word = new COM("word.Application") or die("Could not initialise Object");
        $word->Documents->Open($filenamedoc);
        $word->ActiveDocument->ExportAsFixedFormat($filenamepdf, 17, false, 0, 0, 0, 0, 7, true, true, 2, true, true, false);
        $word->Quit(false);
        unset($word);
        
        // 在页面中显示生成的pdf
        // header('Content-type: application/pdf');
        // header('filename='.$filenamepdf);
        // readfile($filenamepdf);
        
        return '转换成功';
    }
    
    // 测试运行
    // echo xmsb_wordToPdf('./word/word1.docx', './pdf/output1.pdf');
    
    function word2html($wordname,$htmlname)  
    {  
        $word = new COM("word.application") or die("Unable to instanciate Word");  
        $word->Visible = 1;  
        $word->Documents->Open($wordname);  
        $word->Documents[1]->SaveAs($htmlname,8);  
        $word->Quit();  
        $word = null;  
        unset($word);  
        return '转换成功';
    }  
     
    // word2html('./word/cs.docx', './pdf/cs.html'); 
    
    require 'vendor/autoload.php';
    
    function changehtml($word,$htmlname,$url){
        $phpWord = PhpOfficePhpWordIOFactory::load($word);
    
        //增加图片
        $image = __DIR__.$url;
        
        $section = $phpWord->addSection();
        $section->addImage($image,array("width"=>100,"height"=>100,"position"=>"absolute","bottom"=>"0px"));
    
        $xmlWriter = PhpOfficePhpWordIOFactory::createWriter($phpWord, "HTML");
        $xmlWriter ->save($htmlname);
    }
    
    changehtml('./word/wo.docx', './pdf/wo.html','./word/gz.png'); 
    ?>
    

    转载:https://blog.csdn.net/weixin_42971363/article/details/101156195

  • 相关阅读:
    codeforces 820 D. Mister B and PR Shifts(思维)
    codeforces 820 C. Mister B and Boring Game(找规律)
    玲珑杯 1137
    codeforces 817 D. Imbalanced Array(单调栈+思维)
    Atcoder D
    Atcoder C
    Atcode B
    codeforces 816 E. Karen and Supermarket(树形dp)
    codeforces 816 D. Karen and Test(逆元+思维+组合数)
    codeforces 816 C. Karen and Game(模拟+思维)
  • 原文地址:https://www.cnblogs.com/corvus/p/14267948.html
Copyright © 2020-2023  润新知