Git Product home page Git Product logo

Comments (28)

arcanedev-maroc avatar arcanedev-maroc commented on August 26, 2024 9

I'm still waiting for this feature since 2014 😭

from phpword.

Ehaver282 avatar Ehaver282 commented on August 26, 2024 2

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

from phpword.

Worst45 avatar Worst45 commented on August 26, 2024 1

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.

from phpword.

barryvdh avatar barryvdh commented on August 26, 2024

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

from phpword.

SergeC avatar SergeC commented on August 26, 2024

it sounds like project is dead.

from phpword.

barryvdh avatar barryvdh commented on August 26, 2024

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.

from phpword.

ivanlanin avatar ivanlanin commented on August 26, 2024

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?

from phpword.

ivanlanin avatar ivanlanin commented on August 26, 2024

Or 0.7.7 :)

from phpword.

Progi1984 avatar Progi1984 commented on August 26, 2024

Moved to the version 0.7.7 😉

from phpword.

 avatar commented on August 26, 2024

Please, assign it to me.

from phpword.

andrew-kzoo avatar andrew-kzoo commented on August 26, 2024

Thank you! 👍

from phpword.

ivanlanin avatar ivanlanin commented on August 26, 2024

@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.

from phpword.

gabrielbull avatar gabrielbull commented on August 26, 2024

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

from phpword.

ivanlanin avatar ivanlanin commented on August 26, 2024

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

from phpword.

Progi1984 avatar Progi1984 commented on August 26, 2024

@RomanSyroeshko assigned

from phpword.

fbraz3 avatar fbraz3 commented on August 26, 2024

+1

from phpword.

itnolimit avatar itnolimit commented on August 26, 2024

+1

from phpword.

n-osennij avatar n-osennij commented on August 26, 2024

+1

from phpword.

ProdigyMaster avatar ProdigyMaster commented on August 26, 2024

+1

from phpword.

nickshatilo avatar nickshatilo commented on August 26, 2024

+1

from phpword.

georgeenciu avatar georgeenciu commented on August 26, 2024

+1

from phpword.

kisabelle avatar kisabelle commented on August 26, 2024

+1

from phpword.

gruessung avatar gruessung commented on August 26, 2024

+1

from phpword.

BeachyHeadCode avatar BeachyHeadCode commented on August 26, 2024

+1

from phpword.

ivanfr90 avatar ivanfr90 commented on August 26, 2024

+1

from phpword.

xenofx avatar xenofx commented on August 26, 2024

+1

from phpword.

Apteryks avatar Apteryks commented on August 26, 2024

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.

from phpword.

n-osennij avatar n-osennij commented on August 26, 2024

+1

from phpword.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.