PHPOffice / PHPWord

A pure PHP library for reading and writing word processing documents
https://phpoffice.github.io/PHPWord/
Other
7.16k stars 2.68k forks source link

How to get numbered headings in ListItemRun section? #2532

Open shangdev opened 6 months ago

shangdev commented 6 months ago

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?

Progi1984 commented 6 months ago

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

shangdev commented 6 months ago

@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: [ '标题三', '标题四', '标题五', ... ]