PHP将上传word文件,转化为Html格式,(多种转换方式)
2021-03-03 14:25
标签:主机 git not __name__ sts offic write otto ice 转载:https://blog.csdn.net/weixin_42971363/article/details/101156195 PHP将上传word文件,转化为Html格式,(多种转换方式) 标签:主机 git not __name__ sts offic write otto ice 原文地址:https://www.cnblogs.com/corvus/p/14267948.html方法一: 通过PHPOffice(推荐)
1: composer require phpoffice/phpword
```
/* 通过composer安装 PHPOffice
需要
或者用中国镜像:https://www.phpcomposer.com/
*/
```
2: 安装成功可看到 vendor文件夹
3: 使用方法 :
```
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文档
```
例
src="http://view.officeapps.live.com/op/view.aspxsrc=newteach.pbworks.com%2Ff%2Fele%2Bnewsletter.docx"
方法四:通过com组件
需要所在宿主机,有offic的环境,Linxu下不能使用
(不推荐)
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 = \PhpOffice\PhpWord\IOFactory::load($word);
//增加图片
$image = __DIR__.$url;
$section = $phpWord->addSection();
$section->addImage($image,array("width"=>100,"height"=>100,"position"=>"absolute","bottom"=>"0px"));
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, "HTML");
$xmlWriter ->save($htmlname);
}
changehtml(‘./word/wo.docx‘, ‘./pdf/wo.html‘,‘./word/gz.png‘);
?>
文章标题:PHP将上传word文件,转化为Html格式,(多种转换方式)
文章链接:http://soscw.com/index.php/essay/59543.html