Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get numbered headings in ListItemRun section? #2532

Open
shangdev opened this issue Dec 23, 2023 · 2 comments
Open

How to get numbered headings in ListItemRun section? #2532

shangdev opened this issue Dec 23, 2023 · 2 comments
Milestone

Comments

@shangdev
Copy link

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
Copy link
Member

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

@shangdev
Copy link
Author

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

@Progi1984 Progi1984 added this to the 2.0.0 milestone Dec 25, 2023
@Progi1984 Progi1984 modified the milestones: 1.3.0, 1.3.1 Aug 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants