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>
以上就是php将word转换为html格式代码分析的详细内容,更多关于php将word转换为html格式的方法的资料请关注其它相关文章!
推荐阅读
-
分享php代码将360浏览器导出的favdb的sqlite数据库文件转换为html
-
php将word转换为html格式代码分析
-
php ucwords 函数将字符串中每个单词的首字符转换为大写实现代码 words下载 word下载 sight words
-
html table表数据转Json格式示例代码分析
-
分享php代码将360浏览器导出的favdb的sqlite数据库文件转换为html_php实例
-
Java 将Word文件转换为HTML格式文件
-
分享php代码将360浏览器导出的favdb的sqlite数据库文件转换为html
-
分享php代码将360浏览器导出的favdb的sqlite数据库文件转换为html_PHP
-
将RTF格式的文件转成HTML并在网页中显示的代码_php基础
-
使用php将word等office文件转换为pdf格式