1.composer安装相关的包文件

composer require phpoffice/phpword

2.代码

<?php
namespace app\controller;use app\BaseController;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\IOFactory;class Word extends BaseController
{public function index(){$file ="./1.docx";if ($this->getExt($file) != "docx") {return false;}$result = $this->readWordToHtml($file);return show(1,"OK",$result);}public function  getExt( $file_name ){$extend  =  pathinfo ( $file_name );$extend  =  strtolower ( $extend [ "extension" ]);return   $extend ;}public function  getFilename( $file_name ){$pathinfo  =  pathinfo ( $file_name );$filename  =   ( $pathinfo [ "filename" ]);return   $filename ;}public function readWordToHtml($source){$phpWord = \PhpOffice\PhpWord\IOFactory::load($source);$html = '';foreach ($phpWord->getSections() as $section) {foreach ($section->getElements() as $ele1) {
//                $paragraphStyle = $ele1->getParagraphStyle();
//                if ($paragraphStyle) {
//                    $html .= '<p style="text-align:'. $paragraphStyle->getAlignment() .';text-indent:20px;">';
//                } else {
//                    $html .= '<p>';
//                }if ($ele1 instanceof \PhpOffice\PhpWord\Element\TextRun) {foreach ($ele1->getElements() as $ele2) {if ($ele2 instanceof \PhpOffice\PhpWord\Element\Text) {$html .= mb_convert_encoding($ele2->getText(), 'GBK', 'UTF-8');
//                            $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')
//                            );} elseif ($ele2 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>';}}return mb_convert_encoding($html, 'UTF-8', 'GBK');}}

3.结果:

php读取word中的内容相关推荐

  1. 自动化处理--python读取word中表格内容

    from operator import length_hint from sqlite3 import DatabaseError import docx from docx import Docu ...

  2. 在Unity中实现读取word中的内容并打印到界面

    先导入NPOI动态库,前面的文章中有,自取. using System.Collections.Generic; using System.IO; using NPOI.XWPF.UserModel; ...

  3. python读取word指定内容_python读取word 中指定位置的表格及表格数据

    1.Word文档如下: 2.代码 # -*- coding: UTF-8 -*- from docx import Document def readSpecTable(filename, specT ...

  4. Python 批量提取 Word 中表格内容,一键写入 Excel

    关注公众号:[小张Python],为你准备了 50+ 本Python 精品电子书籍 与 50G + 优质视频学习资料,后台回复关键字:1024 即可获取:如果对博文内容有什么疑问,后台添加作者[个人微 ...

  5. java 操作 word 表格和样式,java读取word表格中的表格 java如何读取word中的excel表格数据...

    Java 利用poi 可以直接读取word中的表格保持样式生1.读取word 2003及word 2007需要的jar包 读取 2003 版本(.doc)的word文件相对来说比较简单,只需要 poi ...

  6. java全文检索word中的内容_java获取word里面的文本

    需求场景 开发的web办公系统如果需要处理大量的Word文档(比如有成千上万个文档),用户一定提出查找包含某些关键字的文档的需求,这就要求能够读取 word 中的文字内容,而忽略其中的文字样式.表格. ...

  7. python中读取word信息_Python实现批量读取word中表格信息的方法

    本文实例讲述了Python实现批量读取word中表格信息的方法.分享给大家供大家参考.具体如下: 单位收集了很多word格式的调查表,领导需要收集表单里的信息,我就把所有调查表放一个文件里,写了个py ...

  8. java 静态资源变量_Java中读取配置文件中的内容,并将其赋值给静态变量的方法...

    项目开发中某个功能需要抽取成方法写成一个工具类,提供给别人使用.写过工具类的人都知道,工具类中的方法一般都是静态方法,可以直接使用类名点方法名调用, 使用很方便,比如判断某个对象是否为空的方式Obje ...

  9. python处理表格数据-python读取word 中指定位置的表格及表格数据

    1.Word文档如下: 2.代码 # -*- coding: UTF-8 -*- from docx import Document def readSpecTable(filename, specT ...

最新文章

  1. 华为精益敏捷专家:DevOps转型中的那些坑
  2. 把表格的一列生成数组
  3. 程序员的“鱿鱼游戏”,你能活到第几关?
  4. 北大计算机考研考什么时候,北大计算机考研复习计划有哪些
  5. leetcode -- 3.无重复字符的最长子串
  6. 数据中台应用实战50篇(一)-企业级数据中台的建设方法架构和技术栈
  7. 割平面法只能求解纯整数规划吗_【DMC第四讲】整数规划
  8. X Window Messing With The Mouse Cursor
  9. 支持医学研究的Apple开源移动框架
  10. DbVisualizer的Driver连接Oracle Thin选项不可选
  11. java linux和windows下文件路径间隔符的写法——解决linux下程序在windows下运行时的上传文件出错问题...
  12. Python开发者必知的13个Python GUI库
  13. idea 修改hosts
  14. Gradle插件 protobuf自动编译
  15. 计算机ps基础考试题,2014计算机一级考试PS及基础模拟试题
  16. java获取发件人_Java实现伪造发件人发送邮件
  17. 考试 --微信小程序学习用demo:考证通,在线题库类小程序
  18. 浏览器打开html不加载css样式,ie怎么加载不了css样式?
  19. [DSP 日常记录] #1 冯诺依曼结构、哈佛结构与改进型哈佛结构
  20. windows如何设置全角半角切换

热门文章

  1. comsol稀物质传递_什么是质量传递?
  2. mysql varchar最多可以存多少汉字_MySql的这几个坑你踩过没?真是防不胜防!
  3. 五、Go语言复合数据类型(下)
  4. 微信小程序学习笔记(四)
  5. webpack 4.14.0 版本太高,无法执行相关指令,(将webpack高版本切换到低版本)--直接覆盖
  6. NLP领域最近比较火的Prompt,能否借鉴到多模态领域?一文跟进最新进展
  7. 图灵测试70载,回顾对话机器人的经典实践和最新进展
  8. 人脸静默活体检测最新综述
  9. NLP领域近期有哪些值得读的开源论文?
  10. AliExpress:在检索式问答系统中应用迁移学习 | PaperDaily #24