使用phpoffice/phpword读取word内容

单廷珪
• 阅读 1268

一:phpoffice/phpword安装

composer require phpoffice/phpword

phpword的GitHub地址:https://github.com/PHPOffice/PHPWord

phpword文档地址:https://phpword.readthedocs.io/en/latest/

二:加载word文档

$word = \PhpOffice\PhpWord\IOFactory::load("xxx")

三:获取word所有节点

$sections = $word->getSections()

四:获取word所有段落

$section->getElements()

五:判断文本元素类型

if ($element instanceof \PhpOffice\PhpWord\Element\TextRun) {
    //文本元素
} else if ($element instanceof \PhpOffice\PhpWord\Element\Table) {
    //表格元素
}

六:获取word文本内容

$node->getText()

七:获取word图片

//获取图片编码
$imageData = $node->getImageStringData(true);
//添加图片html显示标头
$imageData = 'data:' . $node->getImageType() . ';base64,' . $imageData;

八:读取word内容示例

/**
 * 获取word文档内容
 * @param string $wordPath
 * @return array
 */
public function getWord($wordPath = '')
{
    //加载word文档,使用phpword处理
    $word = \PhpOffice\PhpWord\IOFactory::load($wordPath);
    return $this->getNodeContent($word);
}

/**
 * 根据word主节点获取分节点内容
 * @param $word
 * @return array
 */
public function getNodeContent($word)
{
    $return = [];
    //分解部分
    foreach ($word->getSections() as $section)
    {
        if ($section instanceof \PhpOffice\PhpWord\Element\Section) {
            //分解元素
            foreach ($section->getElements() as $element)
            {
                //文本元素
                if ($element instanceof \PhpOffice\PhpWord\Element\TextRun) {
                    $text = '';
                    foreach ($element->getElements() as $ele) {
                        $text .= $this->getTextNode($ele);
                    }
                    $return[] = $text;
                }
                //表格元素
                else if ($element instanceof \PhpOffice\PhpWord\Element\Table) {
                    foreach ($element->getRows() as $ele)
                    {
                        $return[] = $this->getTableNode($ele);
                    }
                }
            }
        }
    }
    return $return;
}

/**
 * 获取文档节点内容
 * @param $node
 * @return string
 */
public function getTextNode($node)
{
    $return = '';
    //处理文本
    if ($node instanceof \PhpOffice\PhpWord\Element\Text)
    {
        $return .= $node->getText();
    }
    //处理图片
    else if ($node instanceof \PhpOffice\PhpWord\Element\Image)
    {
        $return .= $this->pic2text($node);
    }
    //处理文本元素
    else if ($node instanceof \PhpOffice\PhpWord\Element\TextRun) {
        foreach ($node->getElements() as $ele) {
            $return .= $this->getTextNode($ele);
        }
    }
    return $return;
}

/**
 * 获取表格节点内容
 * @param $node
 * @return string
 */
public function getTableNode($node)
{
    $return = '';
    //处理行
    if ($node instanceof \PhpOffice\PhpWord\Element\Row) {
        foreach ($node->getCells() as $ele)
        {
            $return .= $this->getTableNode($ele);
        }
    }
    //处理列
    else if ($node instanceof \PhpOffice\PhpWord\Element\Cell) {
        foreach ($node->getElements() as $ele)
        {
            $return .= $this->getTextNode($ele);
        }
    }
    return $return;
}

/**
 * 处理word文档中base64格式图片
 * @param $node
 * @return string
 */
public function pic2text($node)
{
    //获取图片编码
    $imageData = $node->getImageStringData(true);
    //添加图片html显示标头
    $imageData = 'data:' . $node->getImageType() . ';base64,' . $imageData;
    $return = '<img src="'.$imageData.'">';
    return $return;
}
调用方法:
$docx = 'XXX.docx';
$word  = $this->getWord($docx);
点赞
收藏
评论区
推荐文章
blmius blmius
4年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
美凌格栋栋酱 美凌格栋栋酱
10个月前
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(
Wesley13 Wesley13
4年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Wesley13 Wesley13
4年前
java中比较两个时间的差值
项目背景1.某篇文稿的发布时间是publishDate,例如:2020072118:00:41。2.现要求判断该篇文稿的发布时间是否在近30天之内。publicstaticlongdayDiff(DatecurrentDate,DatepublishDate){LongcurrentTimecurrentDat
Jacquelyn38 Jacquelyn38
4年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Wesley13 Wesley13
4年前
thinkphp整合phpword
穿插一段:PHPExcel 认识一人问我phpExcel 执行时出现各种问题  要不就是空的,要不就是浏览器一直刷新,要不就是出现部分数据,有部分数据出不来;经过排查发现是数据太多的问题 执行2万多条数据时出现问题limit5000就没问题后来增大执行时间解决了 !(ht
Stella981 Stella981
4年前
KaliTools说明书+BurpSuit实战指南+SQL注入知识库+国外渗透报告
!(https://oscimg.oschina.net/oscnet/d1c876a571bb41a7942dd9752f68632e.gif"15254461546.gif")0X00KaliLinux Tools中文说明书!(https://oscimg.oschina.net/oscnet/
Stella981 Stella981
4年前
PHPWord导出word文档
最近接了个把数据导出到word文档的需求,之前一直都是使用PHPExcel库导出excel的,还是头次接到导出到word文档的需求,我想既然有PHPExcel,那么肯定也会有PHPWord库吧,在网上一搜,还真有!而且都是phpoffice家的。看了下文档,最终决定使用模板的方式来导出数据,感觉也是最简单的一种方式了。过程如下:使用composer下
Wesley13 Wesley13
4年前
MySQL总结(十一)子查询
!(https://oscimg.oschina.net/oscnet/upa344f41e81d3568e3310b5da00c57ced8ea.png)子查询1\.什么是子查询需求:查询开发部中有哪些员工selectfromemp;通
Wesley13 Wesley13
4年前
PHP创建多级树型结构
<!lang:php<?php$areaarray(array('id'1,'pid'0,'name''中国'),array('id'5,'pid'0,'name''美国'),array('id'2,'pid'1,'name''吉林'),array('id'4,'pid'2,'n
Python进阶者 Python进阶者
1年前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这