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 editing Header? #2528

Open
MixanM opened this issue Dec 13, 2023 · 1 comment
Open

How to editing Header? #2528

MixanM opened this issue Dec 13, 2023 · 1 comment

Comments

@MixanM
Copy link

MixanM commented Dec 13, 2023

Hello, I'm facing a problem - I can't read data from the header of my document. I have a table header in it, I tried different methods - an array is returned to me, but when reading data from there, I get a problem, please help.

$source = "uploads/text.docx";
$objReader = \PhpOffice\PhpWord\IOFactory::createReader('Word2007');
$phpWord = $objReader->load($source);
$headers = $section->getHeaders();
$header1 = $headers[1]; // note that the first index is 1 here (not 0)
$elements = $header1->getElements();

/** its dont work*/
$element1 = $elements[0]; // and first index is 0 here normally
// for example manipulating simple text information ($element1 is instance of Text object)
$element1->setText("This is my text addition - old part: " . $element1->getText());
/** its dont work*/
Fatal error: Uncaught Error: Call to undefined method PhpOffice\PhpWord\Element\TextBreak::setText() 

Header into my 'docx' file
image
how do I get the value from the 5th and 6th column ?

@Akashpaila
Copy link

The error you encountered was due to trying to call setText() on a TextBreak element, which doesn't support text modification. The correct approach is to target the specific table element.

Try the following method which may work correctly

Access the table within the headers
$table = $header1->getElements()[0];

Access the rows and cells
$rows = $table->getRows();
$targetRow = $rows[0];

$cells = $targetRow->getCells();
$cell5 = $cells[4]; // 5th column
$cell6 = $cells[5]; // 6th column

Extract the text
$value5 = $cell5->getText();
$value6 = $cell6->getText();

echo "5th column value: $value5\n";
echo "6th column value: $value6\n";

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