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

import multiple docx templates into the same document #8

Open
SergeC opened this issue Aug 16, 2012 · 28 comments
Open

import multiple docx templates into the same document #8

SergeC opened this issue Aug 16, 2012 · 28 comments

Comments

@SergeC
Copy link
Contributor

SergeC commented Aug 16, 2012

described here http:https://phpword.codeplex.com/discussions/316482

Also add function to merge multiple docx files into one docx. description here http:https://www.phpdocx.com/documentation/api-documentation/merge-two-word-documents


Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

@barryvdh
Copy link

barryvdh commented Sep 6, 2012

I would also like to import a template, just for one section (for generating multiple invoices in 1 file)

@SergeC
Copy link
Contributor Author

SergeC commented Sep 6, 2012

it sounds like project is dead.

@barryvdh
Copy link

barryvdh commented Sep 6, 2012

Do you know of an active fork or good alternative?
Op 6 sep. 2012 22:21 schreef "SergeC" [email protected] het
volgende:

it sounds like project is dead.


Reply to this email directly or view it on GitHubhttps://github.com//issues/8#issuecomment-8346006.

@ivanlanin
Copy link
Contributor

Well, looks like the project is alive again :) Once the reader is build (see #70), your need is easier to implement. @Progi1984, shouldn't we put this to 0.8?

@ivanlanin
Copy link
Contributor

Or 0.7.7 :)

@Progi1984 Progi1984 modified the milestones: 0.7.7, 0.7.1 Mar 10, 2014
@Progi1984
Copy link
Member

Moved to the version 0.7.7 😉

@ghost
Copy link

ghost commented Mar 11, 2014

Please, assign it to me.

@andrew-kzoo
Copy link
Contributor

Thank you! 👍

@Progi1984 Progi1984 modified the milestones: 0.9.3, 0.9.5 Mar 14, 2014
@ivanlanin
Copy link
Contributor

@Progi1984 Can we start assigning issues? I see that you've assigned some issues to you. We also love to have assignments :) I think @RomanSyroeshko is interested in this one.

@gabrielbull
Copy link
Member

Can't assign to people not in the team, maybe you could request @Progi1984 to be part of the team

@ivanlanin
Copy link
Contributor

Ah, I see. Ok. Thanks. I'll submit the request.

@Progi1984
Copy link
Member

@RomanSyroeshko assigned

@Ehaver282
Copy link

Has there been any progress with copying a template to new section in the same document?

@ghost ghost removed their assignment May 21, 2016
@Worst45
Copy link

Worst45 commented Aug 29, 2016

I managed to import some Docx content by playing with AbstractContainer.php. The goal is to integrate our new elements to private variable AbstractContainer::$elements.

For this, I needed to add a new public method, that would take an imported element and:

  • reassign current PhpWord as parent container,
  • walk on all sub-elements to reassign indexes and ids

In AbstractContainer.php:

public function addElementsFromAnotherPhpWord(array $elements) {
    $count = count($elements);
    for ($i = 0; $i < $count; ++$i) {
        $element = $elements[$i];
        $element->setParentContainer($this);
        $this->reassignElementProperties($this, $element);
        $this->elements[] = $element;
    }
}
protected function reassignElementProperties($parent, $element) {
    // Set parent container (code from addElement() )
    $element->setParentContainer($parent);
    $element->setElementIndex($parent->countElements() + 1);
    $element->setElementId();

    // Recursive on sub-elements
    $count = count($element->elements);
    for ($i = 0; $i < $count; ++$i) {
        $this->reassignElementProperties($element, $element->elements[$i]);
    }
}

To call the method:

$PHPWord = new \PhpOffice\PhpWord\PhpWord();
$section = $PHPWord->addSection();

$docImport = \PhpOffice\PhpWord\IOFactory::load('/absolute/path.docx');
$sectionsImport = $docImport->getSections();
$sectionsCount = count($sectionsImport);
for ($iSection = 0; $iSection < $sectionsCount; ++$iSection) {
    $curSection = $sectionsImport[$iSection];
    $elements = $curSection->getElements();
    $section->addElementsFromAnotherPhpWord($elements);
}

I share this as a first guess on how the import could be done. This is not perfect at all! This does not for example reassign global styles ids. You will only have 'paragraph' styles.

@arcanedev-maroc
Copy link

I'm still waiting for this feature since 2014 😭

@fbraz3
Copy link

fbraz3 commented Feb 3, 2017

+1

1 similar comment
@itnolimit
Copy link

+1

@n-osennij
Copy link

+1

@ProdigyMaster
Copy link

+1

7 similar comments
@nickshatilo
Copy link

+1

@georgeenciu
Copy link

+1

@kisabelle
Copy link
Contributor

+1

@gruessung
Copy link

+1

@BeachyHeadCode
Copy link

+1

@ivanfr90
Copy link

+1

@xenofx
Copy link

xenofx commented Jan 17, 2021

+1

@Apteryks
Copy link

Apteryks commented Apr 21, 2021

Thanks for sharing this! I thought I was about to get away with defining these two procedures locally:

/**
 * Attach elements of a different PhpWord object to a container
 * object.
 *
 * @param \PhpOffice\PhpWord\Element\AbstractElement
 * @param array \PhpOffice\PhpWord\Element\AbstractElement
 */
// Adapted from: https://github.com/PHPOffice/PHPWord/issues/8#issuecomment-243126241.
function addElementsFromAnotherPhpWord($parent, array $elements) {
    foreach ($elements as $element) {
        reassignElementProperties($parent, $element);
        $parent->elements[] = $element;
    }
}

/**
 * Recursively adjust the parent containers of an element.
 *
 * @param \PhpOffice\PhpWord\Element\AbstractElement
 * @param array(\PhpOffice\PhpWord\Element\AbstractElement)
 */
// Adapted from: https://github.com/PHPOffice/PHPWord/issues/8#issuecomment-243126241.
function reassignElementProperties($parent, $element) {
    // Set parent container (similar to what
    // AbstractElement::addElement, a protected method, does).
    $element->setParentContainer($parent);
    $element->setElementIndex($parent->countElements() + 1);
    $element->setElementId();

    // Recurse sub-elements.
    foreach ($element->elements() as $child) {
        reassignElementProperties($element, $child);
    }
}

But it turns out that this doesn't fly because the elements property of the AbstractElements is private, so elements cannot be added with $parent->elements[] = $element. Another failed experiment I conducted was this:

function addElementsToSection($section, array $elements) {
    foreach ($elements as $element) {
        reassignElementProperties($section, $element);
        // The elements array is protected... so workaround this by
        // inserting a new paragraph element and overriding that fresh
        // element with ours.
        $new_element = $section->addText('dummy');
        $new_element = $element;
    }
}

Thinking I may be able to mutate the element object. That also didn't have the intended effect (the document would contain 'dummy' paragraphs rather than the $element I thought I had overridden it with). Perhaps that's still possible somehow? I'm no PHP programmer.

@github-actions github-actions bot added the Stale label Nov 17, 2022
@n-osennij
Copy link

+1

@github-actions github-actions bot removed the Stale label Nov 18, 2022
@PHPOffice PHPOffice deleted a comment from github-actions bot Jan 28, 2023
@Progi1984 Progi1984 modified the milestones: 1.2.0, Later Nov 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests