ASCII码 ASCII码

php将word转换为html格式代码分析

发布于:2022-03-07 10:57:17  栏目:技术文档

在本篇文章里小编给大家整理了一篇关于php将word转换为html格式的方法的实例内容,有需要的朋友们可以跟着学习参考下。

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

1、通过PHPOffice

1: composer require phpoffice/phpword

/ 通过composer安装 PHPOffice需要科学上网或者用中国镜像:https://www.phpcomposer.com/ /

2: 安装成功可看到 vendor文件夹

3: 使用方法 :

require ‘vendor/autoload.php’;$phpWord = \PhpOffice\PhpWord\IOFactory::load(‘./4.docx’);$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, “HTML”);$xmlWriter ->save(‘./ceshi.htm’);

4:官方

/官方案例:https://phpword.readthedocs.io/en/latest/general.htmlgithub: https://github.com/PHPOffice/PhpSpreadsheet /

2、通过python

  1. from win32com import client as wc
  2. import sys
  3. def saveHtm():
  4. # print(wordPath)
  5. # print(htmPath)
  6. wordPath='E:/1.doc'
  7. htmPath='E:/1.htm'
  8. word = wc.Dispatch('Word.Application')
  9. print(word)
  10. doc = word.Documents.Open('E:/1.doc')
  11. doc.SaveAs("E:/1.htm", 8) //转化为htm格式
  12. doc.SvaeAs("E:/1.fpt",17)
  13. doc.Close()
  14. word.Quit()
  15. if __name__ == '__main__':
  16. saveHtm()

3、同时Offic API直接在网页显示word文档。

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

4、通过com组件

需要所在宿主机,有offic的环境,Linxu下不能使用实例扩展:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  6. <title>接收上传文件</title>
  7. <?php
  8. $conn = @new COM("ADODB.Connection");
  9. $connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" . realpath("person.mdb");
  10. $conn->Open($connstr);
  11. $uploaddir = 'uploads/';
  12. if(!is_dir($uploaddir)){
  13. mkdir($uploaddir);
  14. }
  15. $filename =$_FILES['filename']['name'];
  16. $filename =substr($_FILES['filename']["name"],0,strpos($_FILES['filename']["name"],"."));
  17. echo $filename;
  18. echo "<br>";
  19. $uploadfile = $uploaddir.$filename.substr($_FILES['filename']["name"],strpos($_FILES['filename']["name"],"."));
  20. //目录名.文件名.后缀名
  21. echo $uploadfile;
  22. echo "<br>";
  23. $temploadfile = $_FILES['filename']['tmp_name'];
  24. echo $temploadfile;
  25. echo "<br>";
  26. move_uploaded_file($temploadfile , $uploadfile); //移动文件
  27. $path = $_SERVER['SCRIPT_FILENAME'];
  28. $filepath = $_SERVER["PHP_SELF"];
  29. $path = substr($path,0,strpos($path,$filepath));
  30. echo $path;
  31. echo "<br>";
  32. echo $filepath;
  33. $htmlpath = $path."/shiyan4/".$uploadfile;
  34. echo "<br>";
  35. echo $htmlpath;
  36. word2html($htmlpath);
  37. //$query =@mysql_query( "Insert into $username(fname,file)values('$filename','$uploadfile')")or die("error");
  38. ?>
  39. <?php
  40. //http://tieba.baidu.com/f?kz=13975389
  41. function word2html($wfilepath)
  42. {
  43. $word=new COM("Word.Application") or die("无法打开 MS Word");
  44. $word->visible = 1 ;
  45. $word->Documents->Open($wfilepath)or die("无法打开这个文件");
  46. $htmlpath=substr($wfilepath,0,-4);
  47. $word->ActiveDocument->SaveAs($htmlpath,8);
  48. $word->quit(0);
  49. }
  50. print( "Word转html完成!" );
  51. ?>
  52. </head>
  53. <body>
  54. </body>
  55. </html>
相关推荐
阅读 +