How to convert word to html format in php

藏色散人
Release: 2023-03-11 19:36:01
Original
4751 people have browsed it

php method to convert word to html format: 1. Conversion through PHPOffice; 2. Conversion through python; 3. Display word document directly on the web page through Office API; 4. Conversion through com component.

How to convert word to html format in php

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

phpHow to convert word to html format ?

PHP will convert the uploaded word file into Html format (multiple conversion methods)

Method 1: Through PHPOffice (recommended)

1:    composer require phpoffice/phpword    
                                            /*  通过composer安装 PHPOffice 
                                                需要科学上网
                                               或者用中国镜像:https://www.phpcomposer.com/
                                            */   
 2:  安装成功可看到 vendor文件夹


 3: 使用方法 :
<?php 
    require &#39;vendor/autoload.php&#39;;
    $phpWord = \PhpOffice\PhpWord\IOFactory::load(&#39;./4.docx&#39;);
    $xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, "HTML");
    $xmlWriter ->save(&#39;./ceshi.htm&#39;);

        
   4:官方  
         /*
           官方案例:https://phpword.readthedocs.io/en/latest/general.html
           github: https://github.com/PHPOffice/PhpSpreadsheet
          */
Copy after login

Method 2: Through python

from win32com import client as wc
import sys

def saveHtm():
	# print(wordPath)
	# print(htmPath)
	wordPath=&#39;E:/1.doc&#39;
	htmPath=&#39;E:/1.htm&#39;
	word = wc.Dispatch(&#39;Word.Application&#39;)
	print(word) 
	doc = word.Documents.Open(&#39;E:/1.doc&#39;)
	doc.SaveAs("E:/1.htm", 8)               //转化为htm格式 
    doc.SvaeAs("E:/1.fpt",17)                

	doc.Close() 
	word.Quit()

if __name__ == &#39;__main__&#39;:
	saveHtm()
Copy after login

Method 3: At the same time, the Office API directly displays the word document on the web page

<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"
Copy after login

Method 4: Through com component

需要所在宿主机,有offic的环境,Linxu下不能使用

(不推荐)
Copy after login

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to convert word to html format in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template