欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間: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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論