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

Use style to have a section with 2 columns #22

Closed
papoteur-mga opened this issue Mar 31, 2023 · 2 comments
Closed

Use style to have a section with 2 columns #22

papoteur-mga opened this issue Mar 31, 2023 · 2 comments

Comments

@papoteur-mga
Copy link

I try to build a document with a section inside it to have two columns in this section.
I didn't find a way to specify that the created section has 2 columns. Here my attempt:


title = "My great document"
writer = Document('text')
writer.body.append(Paragraph(title))

writer.body.append(Element.from_tag("text:soft-page-break"))
writer.body.append(Header(1, "New page"))
writer.body.append(Paragraph("A first Paragraph"))


section_style = Style(name="Twocol", 
                              family="section",
                              display_name="Two columns"
                              )
section_style.set_properties(properties={"fo:column-count":"2"}, area="columns")
section = Section(name="Section1", style = section_style)

section.append(Header(2,"Something:"))
section.append(Paragraph("Blabla"))
section.append(Header(2,"Something else:"))
section.append(Paragraph("Blabla"))
section.append(Header(2,"Something else:"))
section.append(Paragraph("Blabla"))
writer.body.append(section)

writer.body.append(Header(1, "End part"))
writer.body.append(Paragraph("A last Paragraph"))        

#section_style =  Element.from_tag(
    #(   '<style:style style:name="Sect1" style:family="section"><style:section-properties style:editable="false"><style:columns fo:column-count="2" fo:column-gap="0.497cm"><style:column style:rel-width="32767*" fo:start-indent="0cm" fo:end-indent="0.248cm"/><style:column style:rel-width="32768*" fo:start-indent="0.248cm" fo:end-indent="0cm"/></style:columns></style:section-properties></style:style>'
        #)
    #)

writer.save("mydoc.odt")

The document is created with the section, but the section doesn't have 2 columns.
I tried also to add a break page
I have also tried to define the style from a tag (see what is commented), without success.
I tried also to add a break page after the title, without more success.
What have I missed?

@jdum
Copy link
Owner

jdum commented Apr 3, 2023

Hi,
I managed to make it work this way:

  • replace lines from "section_style =..." line to "section = Section(... " by:
section_style = Element.from_tag(
    (
        '<style:style style:name="Twocol" style:family="section">'
        '<style:section-properties style:editable="false">'
        '<style:columns fo:column-count="2" fo:column-gap="0.497cm">'
        '<style:column style:rel-width="32767*" fo:start-indent="0cm" fo:end-indent="0.248cm"/>'
        '<style:column style:rel-width="32768*" fo:start-indent="0.248cm" fo:end-indent="0cm"/>'
        "</style:columns>"
        "</style:section-properties>"
        "</style:style>"
    )
)
writer.insert_style(section_style, automatic=True)
section = Section(name="Section1", style="Twocol")

So:

  • I don't know how to make style "by code", I use the Element.from_tag() trick,
  • you need to add "writer.insert_style(...)",
  • and after style is added, use it by name: style="Twocol"

@papoteur-mga
Copy link
Author

@jdum Thanks, this works, indeed.
I missed (at least) the insertion of the style and the reference with its name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants