Page 1 of 1

String replace from a PowerPoint pptx file using docx4j

PostPosted: Thu Sep 10, 2015 5:01 am
by sabir_mostofa
I am trying to replace some placeholder texts from a PowerPoint .pptx file. They are in the format ${key} as described in the github sample code . However the following code only strips the curly braces off but does not replace the "key" with "green". Thanks for the help in advance.

Code: Select all
PresentationMLPackage ppt = (PresentationMLPackage)   OpcPackage.load(inputPptxFile);

    SlidePart slide = ppt.getMainPresentationPart().getSlide(0);
    HashMap h = new HashMap<String, String>();
    h.put("key", "green");

    slide.variableReplace(h);

Re: String replace from a PowerPoint pptx file using docx4j

PostPosted: Thu Sep 10, 2015 8:04 am
by jason
Welcome to the forum!

By way of preliminaries, I see you crossposted to http://stackoverflow.com/questions/3248 ... ing-docx4j

Please don't do that: announces/stackoverflow-t1088.html

Now, in answer to your question, perhaps you're suffering from the infamous 'split run' problem? Please unzip your pptx, and post the XML contents of your slide part here.

Re: String replace from a PowerPoint pptx file using docx4j

PostPosted: Tue Sep 15, 2015 8:55 pm
by sabir_mostofa
Hi,
Thanks for your super fast reply and I am sorry I didn't know the crossposting rule.

I have attached the first silde where the key is in ${key} format and somehow the curly brackets and the key are not in the same xml tag.


Thanks for your help in advance.


Code: Select all
<a:r>
                            <a:rPr lang="de-DE" altLang="de-DE" sz="2400" dirty="0" smtClean="0">
                                <a:solidFill>
                                    <a:schemeClr val="tx2"/>
                                </a:solidFill>
                            </a:rPr>
                            <a:t>}  ${</a:t>
                        </a:r>
                        <a:r>
                            <a:rPr lang="de-DE" altLang="de-DE" sz="2400" dirty="0" err="1" smtClean="0">
                                <a:solidFill>
                                    <a:schemeClr val="tx2"/>
                                </a:solidFill>
                            </a:rPr>
                            <a:t>key</a:t>
                        </a:r>
                        <a:r>
                            <a:rPr lang="de-DE" altLang="de-DE" sz="2400" dirty="0" smtClean="0">
                                <a:solidFill>
                                    <a:schemeClr val="tx2"/>
                                </a:solidFill>
                            </a:rPr>
                            <a:t>} </a:t>
                        </a:r>

Re: String replace from a PowerPoint pptx file using docx4j

PostPosted: Wed Sep 16, 2015 7:57 am
by sabir_mostofa
I also get a warning, says:

Code: Select all
WARN org.docx4j.XmlUtils - Invalid key '</a:t></a:r><a:r><a:rPr lang="de-DE" altLang="de-DE" sz="2400" dirty="false" err="true" smtClean="false"><a:solidFill><a:schemeClr val="tx2"/></a:solidFill></a:rPr><a:t>key</a:t></a:r><a:r><a:rPr lang="de-DE" altLang="de-DE" sz="2400" dirty="false" smtClean="false"><a:solidFill><a:schemeClr val="tx2"/></a:solidFill></a:rPr><a:t>' or key not mapped to a value

Re: String replace from a PowerPoint pptx file using docx4j

PostPosted: Wed Sep 16, 2015 9:35 am
by jason
You need to fix the split run.

You can do that by manually editing the pptx file (ie using a zip tool).

You can configure Powerpoint to make it less likely to split runs by: Options > Proofing > "Check spelling as you type" UNCHECK

There may be other settings which help as well?

Also, avoid altering the formatting of part of the string.

There is code to fix the split run problem for docx (you can Google for that), and something similar could be written for pptx...

Re: String replace from a PowerPoint pptx file using docx4j

PostPosted: Wed Sep 16, 2015 12:25 pm
by sabir_mostofa
You are a life saver. Thank you very much, It's working now.

Re: String replace from a PowerPoint pptx file using docx4j

PostPosted: Wed Sep 16, 2015 6:52 pm
by sabir_mostofa
Another question, is there any way i can get the total number of slides?
Thanks

Re: String replace from a PowerPoint pptx file using docx4j

PostPosted: Wed Sep 16, 2015 7:09 pm
by sabir_mostofa
Got it in the total

Code: Select all
     Parts parts = ppt.getParts();
        Map<PartName, Part> allPartsMap = parts.getParts();

        int total = 0;
        for (PartName partName : allPartsMap.keySet()) {
            if (partName.getName().contains("/ppt/slides/")) {
                total++;
                SlidePart slidePart = (SlidePart) allPartsMap.get(partName);
               
                //Document doc = XmlUtils.marshaltoW3CDomDocument(slidePart.getJaxbElement());
            }
        }