使用PHPOffice/PHPWord實現(xiàn)讀取Word內(nèi)容
一: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é)點
$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顯示標頭
$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é)點獲取分節(jié)點內(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é)點內(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é)點內(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顯示標頭
$imageData = 'data:' . $node->getImageType() . ';base64,' . $imageData;
$return = '<img src="'.$imageData.'">';
return $return;
}調(diào)用方法:
$docx = 'XXX.docx'; $word = $this->getWord($docx);
到此這篇關于使用PHPOffice/PHPWord實現(xiàn)讀取Word內(nèi)容的文章就介紹到這了,更多相關PHPOffice PHPWord讀取Word內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
PHP number_format() 函數(shù)定義和用法
number_format() 函數(shù)通過千位分組來格式化數(shù)字2012-06-06
解析php addslashes()與addclashes()函數(shù)的區(qū)別和比較
本篇文章是對php中的addslashes()與addclashes()函數(shù)的區(qū)別進行了詳細的分析介紹,需要的朋友參考下2013-06-06
PHP正則匹配日期和時間(時間戳轉(zhuǎn)換)的實例代碼
本文介紹下,用php實現(xiàn)正則匹配日期與時間,并進行時間戳轉(zhuǎn)換的例子,有需要的朋友,參考下吧2016-12-12
php安裝php_rar擴展實現(xiàn)rar文件讀取和解壓的方法
這篇文章主要介紹了php安裝php_rar擴展實現(xiàn)rar文件讀取和解壓的方法,涉及php擴展組件的安裝與使用相關操作技巧,需要的朋友可以參考下2016-11-11
解析php中array_merge與array+array的區(qū)別
本篇文章是對php中array_merge與array+array的區(qū)別進行了詳細的分析介紹,需要的朋友參考下2013-06-06

