New line (CR) in variable text ?

Asked by Arnaud on 2013-05-30

(posting again my question as it seems not to have been stored first time ?)

Is it possible, and how, to replace a changed-track or conditional field with a multilines text ? If yes, how do we write the CR (carriage return) symbol that will be set as a new line ?


Question information

English Edit question
Appy Edit question
No assignee Edit question
Solved by:
Last query:
Last reply:

Hi Arnaud,
It is possible to do that with a note:

do text
from text(thisVarCanContainCarriageReturns)


Arnaud (arnaudgeslin) said : #2

Hi Gaetan
The problem is my input data file and text value is CSV like and thus text is not written an multiple lines itself. I wonder if I can write something like
firt line \n second line \n thirst line
and "\n" (or whatelse ever) would be translated as a new line in the odt document.

Arnaud (arnaudgeslin) said : #3

In addition to newline I would also new tab char. I tried \t but it is written as is in the document and not "interpreted".

Arnaud (arnaudgeslin) said : #4

I found that xml specification :
I tried this in a pod comment :
do text
from 'first line<text:line-break>2nd line'

But I get a syntax error when opening the document in OO.

If I do this
do text
from '<text:p>first line<text:span text:style-name="emphasive">dummy</text:span></text:p>'
I get a correct odf file and text is written but the dummy word is not emphasived , nore italic if I try this.

Is it a good idea to get formatted text, and if yes where am I wrong ?

Salut Arnaud,
And if you try:
do text
from 'first line<text:line-break/>2nd line'

This would be XML-compliant.

Do you have a style named "emphasive" in your pod template? Not that if you define this style, and if you do not apply it in any portion of your pod template, when saving the document, LO will remove it, considered as unused.

Oups sorry I meant:

do text
from '<text:p>first line<text:line-break/>2nd line</text:p>'

Arnaud (arnaudgeslin) said : #7

Salut Gaetan

First : Yes you're right, writting it xml-compliant is much better and it works ! \o/ (I just cute/pasted from the xml specification I talk about above, that's strange they did not give an xml-compliant sample)

2nd : ok for the style, I tought "emphasive" and "italic" were kind of built-in styles like in HTML.

I can now insert perfectl-styled text into my document from python values. Just to say : if someone else needs to get his text formatted in odf syntax the simplest way is to :
- write this text in the odt model
- on disk, unzip the odt file in a temp directory
- open the "context.xml" file
- as all the context is on only 1 line, one could use an online xml parser for easier reading (for instance
- find the part of xml code that deals with the text, and cute/paste it into his python code or input data file.

Thanks for your help.

PS : I must say many people here in my company find POD very powerful and useful, many thanks again for it.

Thank you too :)

Lars (lars-0) said : #9

Just FYI:

It's also possible to do this with a quick and dirty hack:

As the <office:body> Tag ist written in content.xml on one line you can just use a finalize function to convert the newlines automatically when rendering you templates. Just pass it as "finalizeFunction" to the renderer.

It is not the most technically elegant solution, but it let's you use newlines in any expression without using any 'do text from' constructs in comments. (Which get confusing really fast if you use too many per page.)


I use this function to do the conversion:

# Convert \n to line breaks
# Superscript Registered sign
# Subscript "Roughness R_a_"
def finalize(folder):
    for fname in ['content.xml', 'styles.xml']:
        fpname = os.path.join(folder, fname)
        content = file(fpname).read()
        lines = content.split("\n")
        body = False
        content = ""
        numlines = len(lines)
        for i, line in enumerate(lines):
            if not body and line.find('<office:body>') == -1 and line.find('<office:master-styles>') == -1:
                content += line + "\n"
                body = True
            if i != numlines -1:
                line += "<text:line-break></text:line-break>"
            content += line

        content = content.replace('®', '<text:span text:style-name="podSup">®</text:span>')
        content = content.replace('oughness Ra', 'roughness R<text:span text:style-name="podSub">a</text:span>')

        file(fpname, 'w').write(content)

Hi all,
Since the freshly published Appy 0.8.4, any carriage return found in a pod expression (track-changed text or field) will be automatically converted to an ODF line break.