php将word转换为html格式代码分析
发布:smiling 来源: PHP粉丝网 添加日期:2022-05-10 10:27:13 浏览: 评论:0
在本篇文章里小编给大家整理了一篇关于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.html
- github: https://github.com/PHPOffice/PhpSpreadsheet
- */
2、通过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()
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下不能使用
实例扩展:
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
- <title>接收上传文件</title>
- <?php
- $conn = @new COM("ADODB.Connection");
- $connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" . realpath("person.mdb");
- $conn->Open($connstr);
- $uploaddir = 'uploads/';
- if(!is_dir($uploaddir)){
- mkdir($uploaddir);
- }
- $filename =$_FILES['filename']['name'];
- $filename =substr($_FILES['filename']["name"],0,strpos($_FILES['filename']["name"],"."));
- echo $filename;
- echo "<br>";
- $uploadfile = $uploaddir.$filename.substr($_FILES['filename']["name"],strpos($_FILES['filename']["name"],"."));
- //目录名.文件名.后缀名
- echo $uploadfile;
- echo "<br>";
- $temploadfile = $_FILES['filename']['tmp_name'];
- echo $temploadfile;
- echo "<br>";
- move_uploaded_file($temploadfile , $uploadfile); //移动文件
- $path = $_SERVER['SCRIPT_FILENAME'];
- $filepath = $_SERVER["PHP_SELF"];
- $path = substr($path,0,strpos($path,$filepath));
- echo $path;
- echo "<br>";
- echo $filepath;
- $htmlpath = $path."/shiyan4/".$uploadfile;
- echo "<br>";
- echo $htmlpath;
- word2html($htmlpath);
- //$query =@mysql_query( "Insert into $username(fname,file)values('$filename','$uploadfile')")or die("error");
- ?>
- <?php
- //http://tieba.baidu.com/f?kz=13975389
- function word2html($wfilepath)
- {
- $word=new COM("Word.Application") or die("无法打开 MS Word");
- $word->visible = 1 ;
- $word->Documents->Open($wfilepath)or die("无法打开这个文件");
- $htmlpath=substr($wfilepath,0,-4);
- $word->ActiveDocument->SaveAs($htmlpath,8);
- $word->quit(0);
- }
- print( "Word转html完成!" );
- ?>
- </head>
- <body>
- </body>
- </html>
Tags: php将word转换为html
- 上一篇:如何解决php domdocument找不到的问题
- 下一篇:最后一页
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)