最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用PHPOffice/PHPWord實(shí)現(xiàn)讀取Word內(nèi)容

 更新時間:2023年07月17日 15:45:32   作者:huaweichenai  
這篇文章主要為大家詳細(xì)介紹了如何使用PHPOffice/PHPWord實(shí)現(xiàn)讀取Word內(nèi)容的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下

一: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所有節(jié)點(diǎn)

$sections = $word->getSections()

四:獲取word所有段落

$section->getElements()

五:判斷文本元素類型

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

六:獲取word文本內(nèi)容

$node->getText()

七:獲取word圖片

//獲取圖片編碼
$imageData = $node->getImageStringData(true);
//添加圖片html顯示標(biāo)頭
$imageData = 'data:' . $node->getImageType() . ';base64,' . $imageData;
八:讀取word內(nèi)容示例
/**
 * 獲取word文檔內(nèi)容
 * @param string $wordPath
 * @return array
 */
public function getWord($wordPath = '')
{
    //加載word文檔,使用phpword處理
    $word = \PhpOffice\PhpWord\IOFactory::load($wordPath);
    return $this->getNodeContent($word);
}
/**
 * 根據(jù)word主節(jié)點(diǎn)獲取分節(jié)點(diǎn)內(nèi)容
 * @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;
}
/**
 * 獲取文檔節(jié)點(diǎn)內(nèi)容
 * @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;
}
/**
 * 獲取表格節(jié)點(diǎn)內(nèi)容
 * @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顯示標(biāo)頭
    $imageData = 'data:' . $node->getImageType() . ';base64,' . $imageData;
    $return = '<img src="'.$imageData.'">';
    return $return;
}

調(diào)用方法:

$docx = 'XXX.docx';
$word  = $this->getWord($docx);

到此這篇關(guān)于使用PHPOffice/PHPWord實(shí)現(xiàn)讀取Word內(nèi)容的文章就介紹到這了,更多相關(guān)PHPOffice PHPWord讀取Word內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

乐山市| 嘉荫县| 临夏县| 登封市| 额济纳旗| 湖口县| 泰和县| 台东市| 缙云县| 鸡西市| 凤翔县| 夏邑县| 鸡泽县| 襄樊市| 远安县| 开化县| 麻江县| 灵丘县| 桑植县| 都兰县| 富阳市| 五指山市| 沁源县| 临邑县| 昭觉县| 宣武区| 黄冈市| 深泽县| 永安市| 浏阳市| 邢台县| 界首市| 丹寨县| 博白县| 大同市| 兰坪| 石门县| 阳春市| 南丰县| 寿宁县| 睢宁县|