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

Word2007 Reader: Title not recognized when it's a list item #2421

Open
rasteiner opened this issue Jul 12, 2023 · 0 comments
Open

Word2007 Reader: Title not recognized when it's a list item #2421

rasteiner opened this issue Jul 12, 2023 · 0 comments

Comments

@rasteiner
Copy link

Describe the Bug

In Word, titles can also be list items (happens when people want to automatically number their headings).
In PHPWord, such "paragraphs" are recognized as List items, but not as titles.

Steps to Reproduce

  1. create a doc in word
  2. write text
  3. apply "Heading 1" style
  4. make the same line a numbered list
  5. save file as listheading.docx
  6. run the following code:
<?php

use PhpOffice\PhpWord\Element\AbstractContainer;
use PhpOffice\PhpWord\Element\AbstractElement;
use PhpOffice\PhpWord\Reader\Word2007;

require __DIR__ . '/vendor/autoload.php';

$reader = new Word2007();
$phpWord = $reader->load('listheading.docx');

function recursive_echo(AbstractElement $el, $depth = 0) {
    echo str_repeat(' ', $depth * 2) . preg_replace('/^PhpOffice\\\\PhpWord\\\\Element\\\\/', '', get_class($el)) . "\n";

    if($el instanceof AbstractContainer) {
        foreach($el->getElements() as $e) {
            recursive_echo($e, $depth + 1);
        }
    }
}

foreach($phpWord->getSections() as $s) {
    recursive_echo($s);
}

Expected Behavior

I'd expect the output to be something like:

Section
  ListItemRun
    Title

Current Behavior

Section
  ListItemRun
    Text

Context

I believe this has something to do with the AbtractPart reader in for Word2007:

In the readParagraph function, the paragraph can either be a ListItem or a Title, but never both:
https://github.com/PHPOffice/PHPWord/blob/77438025265482ddcf050bce520d3c2b51645108/src/PhpWord/Reader/Word2007/AbstractPart.php#L140C9-L163

  • PHP Version: 8.2
  • PHPWord Version: 1.1.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

1 participant