phpword读取word docx文档文本及图片转html格式

embedded/2024/9/21 9:51:24/

最近在做一个PHP读取html" title=word>word文档功能,搜索一圈后决定选择用phphtml" title=word>word第三方组件。
在这里插入图片描述

composer安装phpWord

composer require phpoffice/phphtml" title=word>word

如果你的文件是doc格式,直接另存为一个docx就行了;如果你的doc文档较多,可以下一个批量转换工具:http://www.batchwork.com/en/doc2doc/download.htm

关键点

  • 对齐方式:PhpOffice\PhpWord\Style\Paragraph -> getAlignment()

  • 字体名称:\PhpOffice\PhpWord\Style\Font -> getName()

  • 字体大小:\PhpOffice\PhpWord\Style\Font -> getSize()

  • 是否加粗:\PhpOffice\PhpWord\Style\Font -> isBold()

  • 读取图片:\PhpOffice\PhpWord\Element\Image -> getImageStringData()

  • ba64格式图片数据保存为图片:file_put_contents( i m a g e S r c , b a s e 6 4 d e c o d e ( imageSrc, base64_decode( html">imageSrc,base64decode(imageData))

完整代码

html" title=word>word">require './vendor/autoload.php';html" title=word>word">function docx2html($source)
{$phpWord = \PhpOffice\PhpWord\IOFactory::load($source);$html = '';html" title=word>word">foreach ($phpWord->getSections() html" title=word>word">as $section) {html" title=word>word">foreach ($section->getElements() html" title=word>word">as $ele1) {$paragraphStyle = $ele1->getParagraphStyle();html" title=word>word">if ($paragraphStyle) {$html .= '<p style="text-align:'. $paragraphStyle->getAlignment() .';text-indent:20px;">';} html" title=word>word">else {$html .= '<p>';}html" title=word>word">if ($ele1 html" title=word>word">instanceof \PhpOffice\PhpWord\Element\TextRun) {html" title=word>word">foreach ($ele1->getElements() html" title=word>word">as $ele2) {html" title=word>word">if ($ele2 html" title=word>word">instanceof \PhpOffice\PhpWord\Element\Text) {$style = $ele2->getFontStyle();$fontFamily = mb_convert_encoding($style->getName(), 'GBK', 'UTF-8');$fontSize = $style->getSize();$isBold = $style->isBold();$styleString = '';$fontFamily && $styleString .= "font-family:{$fontFamily};";$fontSize && $styleString .= "font-size:{$fontSize}px;";$isBold && $styleString .= "font-weight:bold;";$html .= sprintf('<span style="%s">%s</span>',$styleString,mb_convert_encoding($ele2->getText(), 'GBK', 'UTF-8'));} html" title=word>word">elseif ($ele2 html" title=word>word">instanceof \PhpOffice\PhpWord\Element\Image) {$imageSrc = 'images/' . md5($ele2->getSource()) . '.' . $ele2->getImageExtension();$imageData = $ele2->getImageStringData(true);// $imageData = 'data:' . $ele2->getImageType() . ';base64,' . $imageData;file_put_contents($imageSrc, base64_decode($imageData));$html .= '<img src="'. $imageSrc .'" style="width:100%;height:auto">';}}}$html .= '</p>';}}html" title=word>word">return mb_convert_encoding($html, 'UTF-8', 'GBK');
}$dir = str_replace('\\', '/', __DIR__) . '/';
$source = $dir . 'test.docx';
html" title=word>word">echo docx2html($source);

补充

很明显,这是一个简陋的html" title=word>word读取示例,只读取了段落的对齐方式,文字的字体、大小、是否加粗及图片等信息,其他例如文字颜色、行高。。。等等信息都忽悠了。需要的话,请自行查看phpWord源码,看\PhpOffice\PhpWord\Style\xxx 和 \PhpOffice\PhpWord\Element\xxx 等类里有什么读取方法就可以了

以下方法直接获取到完整的html

$phpWord = \PhpOffice\PhpWord\IOFactory::load('xxx.docx');
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, "HTML");
$html = $xmlWriter->getContent();

注:html内容里包含了head部分,如果只需要style和body的话,需要自己处理一下;然后图片是base64的,要保存的话,也需要自己处理一下
base64数据保存为图片请参考上面代码

如果只想获取body里的内容,可以参考 \PhpOffice\PhpWord\Writer\HTML\Part\Body 里的 write 方法

$phpWord = \PhpOffice\PhpWord\IOFactory::load('xxxx.docx');
$htmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, "HTML");
$content = '';
html" title=word>word">foreach ($phpWord->getSections() html" title=word>word">as $section) {$writer = html" title=word>word">new \PhpOffice\PhpWord\Writer\HTML\Element\Container($htmlWriter, $section);$content .= $writer->write();
}
html" title=word>word">echo $content;html" title=word>word">exit;

图片的处理的话,暂时没有好办法能在不修改源码的情况下处理好,改源码的话,相关代码在 \PhpOffice\PhpWord\Writer\HTML\Element\Image 里

html" title=word>word">public html" title=word>word">function write()
{html" title=word>word">if (!$this->element html" title=word>word">instanceof ImageElement) {html" title=word>word">return '';}$content = '';$imageData = $this->element->getImageStringData(true);html" title=word>word">if ($imageData !== null) {$styleWriter = html" title=word>word">new ImageStyleWriter($this->element->getStyle());$style = $styleWriter->write();// $imageData = 'data:' . $this->element->getImageType() . ';base64,' . $imageData;$imageSrc = 'images/' . md5($this->element->getSource()) . '.' . $this->element->getImageExtension();// 这里可以自己处理,上传oss之类的file_put_contents($imageSrc, base64_decode($imageData));$content .= $this->writeOpening();$content .= "<img border=\"0\" style=\"{$style}\" src=\"{$imageSrc}\"/>";$content .= $this->writeClosing();}html" title=word>word">return $content;
}

http://www.ppmy.cn/embedded/114532.html

相关文章

JavaScript知识点3

目录 1.JavaScript中有多少个线程&#xff1f; 2.如何判断一个对象是不是空对象&#xff1f; 3.什么事JavaScript时间死区&#xff1f; 4.什么是JSON stringify&#xff1f; 1.JavaScript中有多少个线程&#xff1f; JavaScript中的主线程负责执行代码、处理事件和更新用户…

fiddler抓包06_抓取https请求(chrome)

课程大纲 首次安装Fiddler&#xff0c;抓https请求&#xff0c;除打开抓包功能&#xff08;F12&#xff09;还需要&#xff1a; ① Fiddler开启https抓包 ② Fiddler导出证书&#xff1b; ③ 浏览器导入证书。 否则&#xff0c;无法访问https网站&#xff08;如下图&#xff0…

鹏哥C语言39---分支/循环语句练习:猜数字游戏

#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <time.h> //void fun(int a[]) //因为传过来的是地址&#xff0c;所以应该用一个指针变量来接收&#xff0c;故这里的 a 本质上是个指针变量 //{ // printf("%z…

基于Python+SQLite的课程管理系统

系统需求简介 1.1需求分析 实现一个具体的课程管理系统。按照软件工程思路设计简化的专业课数据库&#xff0c;尽量模拟现有专业课程一个学期的选课排课原型实际情况。&#xff08;注&#xff1a;本系统由本人单独设计、开发完成&#xff09; 1.2 数据结构需求分析 课程管理…

线程调优——调整线程池参数提升程序执行效率

先抛出一个问题&#xff0c;程序开发真的是线程越多效率越高吗&#xff1f;多线程是我们程序开发中必不可少的手段&#xff0c;线程就像“孙悟空”开启了分身术一样&#xff0c;每个分身都在“打妖怪”&#xff0c;那是不是分身越多&#xff0c;“打妖怪”的效率就越高&#xf…

安卓13设置动态显示隐藏第一页的某一项 动态显示隐藏无障碍 android13设置动态显示隐藏第一页的某一项

总纲 android13 rom 开发总纲说明 文章目录 1.前言2.问题分析3.代码分析4.代码修改4.1修改方法14.2修改方法25.编译6.彩蛋1.前言 有时候,我们的设置里面显示的信息,需要根据不同的情况显示不同的信息,例如,动态的显示或者隐藏 “无障碍” 这一项。 2.问题分析 像这个问题…

【Node.js】semver 语义化版本控制

semver&#xff08;语义化版本控制&#xff09;是一种约定式的版本命名规范&#xff0c;它将版本号分为主版本号、次版本号和修订号&#xff0c;并按照 MAJOR.MINOR.PATCH 的格式进行编号。 1&#xff09;版本号释义&#xff1a; MAJOR&#xff08;主版本号&#xff09;&…

艾丽卡的区块链英语小课堂

系列文章目录 IT每日英语&#xff08;三&#xff09; 文章目录 系列文章目录前言1.principle2.efficient3.implement4.accumulated5,occupation6.phases7.validator8.nominated9.commissions10.significantly 前言 欢迎来到艾丽卡的区块链英语小课堂&#xff0c;在这里&…