how to insert line-breaks in python expressions

Asked by Martin Trummer on 2010-11-19

I have one variable in my odt template that I want to assign a pyhton string to
the pyhton string has some linebreaks - but these are not shown in the generated output odt

what should I use to get a linebreak?
'\n', '\r' seem not to work

Question information

English Edit question
Appy Edit question
No assignee Edit question
Solved by:
Gaëtan Delannay
Last query:
Last reply:

Hi, You can't do that with expressions, but you can do it with a statement (in a note). Suppose your string containing linebreaks is called "lbString".
Write a note containing smth like:

do text
from xhtml(lbString.replace('\n', '<br/>'))

(normally this should work. It it doesn't, wrap your string in a <p></p>).

In the future I plan to generarize the "xhtml" function into some "text" function, with an arg "type". You'll then be able to write things like

do xxx
from text(lbString)

In this case it will do exactly what you want to do. And for conversions from Python strings containing xhtml, it will look like:

do xxx
from text(xhtmlString, type='xhtml')

Cheers !
Gaetan Delannay

with using xhtml there are several problems
 1) I loose the original paragraph formatting - xhtml seems to insert a new paragraph
 2) I need to replace special characters:
ine.replace('&', '&#38;').replace('<', '&lt;').replace('>', '&gt;').replace(' ', '&#160;').replace('\n', '<br />')

I can live with the solution for problem 2)
but how could I solve 1)

Hello !
Indeed, your input string must be valid XHTML, so you need to deal with special chars.
And yes indeed, the xhtml function creates a paragraph, it is not possible to avoid this. The only solution is to manage yourself, in the input string, the whole paragraph.

Thanks Gaëtan Delannay, that solved my question.

Luc Saffre (luc-saffre) said : #5

Thanks Martin & Gaëtan... I just wanted to ask the same question :-)

Andres (vandresv) said : #6


I have also problems with linefeeds.
I tried:

do text
from xhtml(mystring.replace('\n','<br/>'))

It overrides my font settings.
How can I specify font and font size?

I tried to wrote 'mystring' as HTML, but unfortunately I'm not very familiar with it so I come up with following which is I think wrong:
mystring = '<html><body><font size="5" face="arial" color="red">This paragraph is in Arial, size 5, and in red text color.</font></body></html>'

So how could I change font settings?

Your question has nothing to do with line breaks, please could you post a new question next time ? Thanks.

POD XHTML->ODT conversion (method "xhtml") can't interpret HTML style and font settings. What you can do is to define a "stylesMapping": make a correspondence between a CSS class and a style you define in your ODT document. More info about styles mapping here: (section "Defining styles mappings").