PHPOffice / PHPWord

A pure PHP library for reading and writing word processing documents

Home Page:https://phpoffice.github.io/PHPWord/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to get numbered headings in ListItemRun section?

shangdev opened this issue · comments

image

As shown above, when I read a word document, I can't get the serial number in front of the ListItemRun text block, e.g. 1,2,3 or one, two, three.

Is there any way to get it?

@shangdev Could you send a sample document for analysis ? Thanks

@Progi1984

Sample document:
testDocx.docx

Sample code:

$list = [];
$source = \PhpOffice\PhpWord\IOFactory::load($filepath)->getSections();
foreach ($source as $s) {
    $elements = $s->getElements();
    foreach ($elements as $element) {
        $class = get_class($element);
	$elname = explode("\\", $class)[3];
	if ($elname === 'ListItemRun') {
            $textArr = getTextElement($element);
            if ($textArr) {
		$list[] = implode('', $textArr);
	    }
        }
    }
}

function getTextElement($element) {
        $elements = $element->getElements();
	if ($elements) {
		$inResult = [];
		foreach ($elements as $inE) {
			$ns = get_class($inE);
			$elName = explode('\\', $ns)[3];
			if ($elName == 'Text') {
				$result[] = $inE->getText();
			} elseif (method_exists($inE, 'getElements')) {
				$inResult = getTextElement($inE);
			}
			if (!is_null($inResult)) {
				$result = array_merge($result, $inResult);
			}
		}
	}

	return count($result) > 0 ? $result : null;
}

echo $list;

Output:
[
'标题三',
'标题四',
'标题五',
...
]