Beispiel #1
0
    def _make_layout(self, doc, layout, firstpage):
        # set paper dimensions
        pageLayout = style.PageLayout(name=u"pl1")
        doc.automaticstyles.addElement(pageLayout)
        plProp = style.PageLayoutProperties(pageheight=str(layout.height),
                                            pagewidth=str(layout.width),
                                            marginleft=str(layout.left),
                                            marginright=str(layout.right),
                                            margintop=str(layout.top),
                                            marginbottom=str(layout.bottom))
        pageLayout.addElement(plProp)

        # add page numbers to the footers
        footer = style.Footer()
        foostyle = style.Style(name="Footer", family="paragraph")
        foostyle.addElement(style.ParagraphProperties(textalign='center'))
        foostyle.addElement(style.TextProperties(fontsize='10pt'))
        doc.automaticstyles.addElement(foostyle)
        p = text.P(stylename=foostyle)
        p.addElement(
            text.PageNumber(selectpage="current",
                            pageadjust=str(firstpage - 1)))
        footer.addElement(p)

        masterpage = style.MasterPage(name=u"Standard",
                                      pagelayoutname=pageLayout)
        masterpage.addElement(footer)
        doc.masterstyles.addElement(masterpage)
Beispiel #2
0
    def alignCenter(self) -> style.ParagraphProperties:
        """
        Align a cell to the center property.

        @return Style Property.
        """

        return style.ParagraphProperties(textalign="center")
Beispiel #3
0
 def test_style(self):
     """ Get a common style with getStyleByName """
     textdoc = OpenDocumentText()
     tablecontents = style.Style(name="Table Contents", family="paragraph")
     tablecontents.addElement(style.ParagraphProperties(numberlines="false", linenumber="0"))
     textdoc.styles.addElement(tablecontents)
     s = textdoc.getStyleByName('Table Contents')
     self.assertEquals((u'urn:oasis:names:tc:opendocument:xmlns:style:1.0', 'style'), s.qname)
Beispiel #4
0
    def alignLeft(self) -> style.ParagraphProperties:
        """
        Align a cell to the left property.

        @return Style Property.
        """

        return style.ParagraphProperties(textalign="left")
Beispiel #5
0
    def testAutomaticStyles(self):
        """ Create a text document with a page layout called "pagelayout"
            Add a master page
            Add an automatic style for the heading
            Check that pagelayout is listed in styles.xml under automatic-styles
            Check that the heading style is NOT listed in styles.xml
            Check that the pagelayout is NOT listed in content.xml
        """
        textdoc = OpenDocumentText()

        parastyle = style.Style(name="Para", family="paragraph")
        parastyle.addElement(
            style.ParagraphProperties(numberlines="false", linenumber="0"))
        parastyle.addElement(
            style.TextProperties(fontsize="24pt", fontweight="bold"))
        textdoc.automaticstyles.addElement(parastyle)

        hpstyle = style.Style(name="HeaderPara", family="paragraph")
        hpstyle.addElement(style.ParagraphProperties(linenumber="0"))
        hpstyle.addElement(
            style.TextProperties(fontsize="18pt", fontstyle="italic"))
        textdoc.automaticstyles.addElement(hpstyle)

        pl = style.PageLayout(name="pagelayout")
        textdoc.automaticstyles.addElement(pl)

        mp = style.MasterPage(name="Standard", pagelayoutname=pl)
        textdoc.masterstyles.addElement(mp)
        h = style.Header()
        hp = text.P(text="header content", stylename=hpstyle)
        h.addElement(hp)
        mp.addElement(h)

        textdoc.text.addElement(text.P(text="Paragraph 1",
                                       stylename=parastyle))

        # Check styles.xml
        s = textdoc.stylesxml()
        self.assertContains(s, u'<style:page-layout style:name="pagelayout"/>')
        self.assertContains(s, u'style:name="HeaderPara"')
        self.assertNotContains(s, u'style:name="Para" ')
        # Check content.xml
        s = textdoc.contentxml()  # contentxml is supposed to yed a byts
        self.assertNotContains(
            s, b'<style:page-layout style:name="pagelayout"/>')
        self.assertContains(s, b'style:name="Para"')
Beispiel #6
0
    def alignRight(self) -> style.ParagraphProperties:
        """
        Align a cell to the right property.

        @return Style Property.
        """

        return style.ParagraphProperties(textalign="right")
Beispiel #7
0
 def test_xstyle(self):
     self.assertRaises(UnicodeDecodeError, style.Style, name="X✗", family="paragraph")
     xstyle = style.Style(name=u"X✗", family=u"paragraph")
     pp = style.ParagraphProperties(padding=u"0.2cm")
     pp.setAttribute(u"backgroundcolor", u"rød")
     xstyle.addElement(pp)
     self.textdoc.styles.addElement(xstyle)
     self.textdoc.save(u"TEST.odt")
     self.saved = True
Beispiel #8
0
    def testTable(self):
        """ Create a presentation with a page layout called MyLayout
        """
        presdoc = OpenDocumentPresentation()
        # We must describe the dimensions of the page
        pagelayout = style.PageLayout(name="MyLayout")
        presdoc.automaticstyles.addElement(pagelayout)
        pagelayout.addElement(
            style.PageLayoutProperties(margin="0cm",
                                       pagewidth="28cm",
                                       pageheight="21cm",
                                       printorientation="landscape"))

        # Every drawing page must have a master page assigned to it.
        masterpage = style.MasterPage(name="MyMaster",
                                      pagelayoutname=pagelayout)
        presdoc.masterstyles.addElement(masterpage)

        # Style for the title frame of the page
        # We set a centered 34pt font with yellowish background
        titlestyle = style.Style(name="MyMaster-title", family="presentation")
        titlestyle.addElement(style.ParagraphProperties(textalign="center"))
        titlestyle.addElement(style.TextProperties(fontsize="34pt"))
        titlestyle.addElement(style.GraphicProperties(fillcolor="#ffff99"))
        presdoc.styles.addElement(titlestyle)

        # Style for the photo frame
        mainstyle = style.Style(name="MyMaster-main", family="presentation")
        presdoc.styles.addElement(mainstyle)

        # Create style for drawing page
        dpstyle = style.Style(name="dp1", family="drawing-page")
        presdoc.automaticstyles.addElement(dpstyle)

        page = draw.Page(stylename=dpstyle, masterpagename=masterpage)
        presdoc.presentation.addElement(page)

        titleframe = draw.Frame(stylename=titlestyle,
                                width="720pt",
                                height="56pt",
                                x="40pt",
                                y="10pt")
        page.addElement(titleframe)
        textbox = draw.TextBox()
        titleframe.addElement(textbox)
        textbox.addElement(text.P(text="Presentation"))

        mainframe = draw.Frame(stylename=mainstyle,
                               width="720pt",
                               height="500pt",
                               x="0pt",
                               y="56pt")
        page.addElement(mainframe)
        mainframe.addElement(table.Table())
Beispiel #9
0
    def testStyle(self):
        """ Create a presentation with a page layout called MyLayout
            Add a presentation style for the title
            Check that MyLayout is listed in styles.xml
        """
        presdoc = OpenDocumentPresentation()
        # We must describe the dimensions of the page
        pagelayout = style.PageLayout(name="MyLayout")
        presdoc.automaticstyles.addElement(pagelayout)
        pagelayout.addElement(
            style.PageLayoutProperties(margin="0cm",
                                       pagewidth="28cm",
                                       pageheight="21cm",
                                       printorientation="landscape"))

        # Every drawing page must have a master page assigned to it.
        masterpage = style.MasterPage(name="MyMaster",
                                      pagelayoutname=pagelayout)
        presdoc.masterstyles.addElement(masterpage)

        # Style for the title frame of the page
        # We set a centered 34pt font with yellowish background
        titlestyle = style.Style(name="MyMaster-title", family="presentation")
        titlestyle.addElement(style.ParagraphProperties(textalign="center"))
        titlestyle.addElement(style.TextProperties(fontsize="34pt"))
        titlestyle.addElement(style.GraphicProperties(fillcolor="#ffff99"))
        presdoc.styles.addElement(titlestyle)

        s = presdoc.stylesxml()
        self.assertContains(
            s,
            '<style:page-layout style:name="MyLayout"><style:page-layout-properties '
        )
        e = ElementParser(s, 'style:page-layout-properties')
        self.assertEqual(e.element, 'style:page-layout-properties')
        self.assertTrue(e.has_value("fo:margin", "0cm"))
        self.assertTrue(e.has_value("fo:page-width", "28cm"))
        self.assertTrue(e.has_value("fo:page-height", "21cm"))
        self.assertTrue(e.has_value("style:print-orientation", "landscape"))

        e = ElementParser(s, 'style:style')
        self.assertTrue(e.has_value("style:name", "MyMaster-title"))
        self.assertTrue(e.has_value("style:display-name", "MyMaster-title"))
        self.assertTrue(e.has_value("style:family", "presentation"))

        self.assertContains(
            s,
            '<style:paragraph-properties fo:text-align="center"/><style:text-properties fo:font-size="34pt"/><style:graphic-properties draw:fill-color="#ffff99"/></style:style></office:styles>'
        )
        e = ElementParser(s, 'style:master-page')
        self.assertTrue(e.has_value("style:name", "MyMaster"))
        self.assertTrue(e.has_value("style:display-name", "MyMaster"))
        self.assertTrue(e.has_value("style:page-layout-name", "MyLayout"))
Beispiel #10
0
 def testAttributeForeign(self):
     """ Test that you can add foreign attributes """
     textdoc = OpenDocumentText()
     standard = style.Style(name="Standard", family="paragraph")
     p = style.ParagraphProperties(
         qattributes={
             (u'http://foreignuri.com', u'enable-numbering'): 'true'
         })
     standard.addElement(p)
     textdoc.styles.addElement(standard)
     s = unicode(textdoc.stylesxml(), 'UTF-8')
     s.index(u"""<?xml version='1.0' encoding='UTF-8'?>\n""")
     s.index(u'xmlns:ns30="http://foreignuri.com"')
     s.index(u'<style:paragraph-properties ns30:enable-numbering="true"/>')
     s.index(
         u'<office:styles><style:style style:name="Standard" style:display-name="Standard" style:family="paragraph">'
     )
Beispiel #11
0
    def testAttributeForeign(self):
        """ Test that you can add foreign attributes """
        textdoc = OpenDocumentText()
        standard = style.Style(name="Standard", family="paragraph")
        p = style.ParagraphProperties(qattributes={(u'http://foreignuri.com',u'enable-numbering'):'true'})
        standard.addElement(p)
        textdoc.styles.addElement(standard)
        s = unicode(textdoc.stylesxml(),'UTF-8')
        s.index(u"""<?xml version='1.0' encoding='UTF-8'?>\n""")
        s.index(u'xmlns:ns41="http://foreignuri.com"')
        s.index(u'<style:paragraph-properties ns41:enable-numbering="true"/>')
        e = ElementParser(s,'style:style')
#        e = ElementParser(u'<style:style style:name="Standard" style:display-name="Standard" style:family="paragraph">')
        self.assertEqual(e.element,'style:style')
        self.assertTrue(e.has_value("style:display-name","Standard"))
        self.assertTrue(e.has_value("style:name","Standard"))
        self.assertTrue(e.has_value("style:family","paragraph"))
                                fontfamilygeneric="swiss",
                                fontpitch="fixed")

#
# Section styles
#
sect = style.Style(name="Sect1", family="section")
sectTable = style.Style(name="SectTable", family="section")

#
# Paragraph styles
#
standard = style.Style(name="Standard", family="paragraph")
standard.addElement(
    style.ParagraphProperties(margintop="0in",
                              marginbottom="0in",
                              punctuationwrap="hanging",
                              linebreak="strict"))
standard.addElement(
    style.TextProperties(color="#000000",
                         fontsize="12pt",
                         fontname="DejaVuSerif",
                         language="en",
                         country="US"))

# textbody is the default for text
textbody = style.Style(name="TextBody", family="paragraph")
textbody.addElement(
    style.ParagraphProperties(marginbottom="0.05in",
                              margintop="0.04in",
                              textalign="left"))
textbody.addElement(
Beispiel #13
0
#
sect = style.Style(name="Sect1", family="section")

sectTable = style.Style(name="SectTable", family="section")

#
# Paragraph styles
#


# textbody is the default for text
textbody = style.Style(name="TextBody", family="paragraph")
textbody.addElement(
    style.ParagraphProperties(
        marginbottom="0.05in", margintop="0.04in", textalign="left",
        punctuationwrap="hanging", linebreak="strict",
        orphans="3", keeptogether="always",

    )
)
textbody.addElement(
    style.TextProperties(
        fontsize="12pt", language="en", country="US",
        fontname="DejaVuSerif"
    )
)

#
# special paragraph styles:  paragraph text styles
#
preformatted = style.Style(name="Preformatted", family="paragraph")
preformatted.addElement(
Beispiel #14
0
    def _make_styles(self, doc, layout):
        """Generate set of styles for the document.

        Parameters
        ----------
        doc : `OpenDocumentText`
            ODT document object.
        layout : `PageLayout`
            ODT page layout.
        """
        styles = {}

        # heading styles, occupies whole page, centered
        h1font = '22pt'
        h1topmrg = (layout.height - layout.top - layout.bottom) * 0.5
        h1topmrg -= Size(h1font)
        h1style = style.Style(name="Heading 1", family="paragraph")
        h1style.addElement(
            style.ParagraphProperties(textalign='center',
                                      breakbefore='page',
                                      margintop=str(h1topmrg)))
        h1style.addElement(
            style.TextProperties(fontsize=h1font, fontweight='bold'))
        doc.styles.addElement(h1style)
        styles['h1'] = h1style

        brstyle = style.Style(name="Break", family="paragraph")
        brstyle.addElement(
            style.ParagraphProperties(textalign='center', breakafter='page'))
        doc.automaticstyles.addElement(brstyle)
        styles['br'] = brstyle

        h2namestyle = style.Style(name="Heading 2 (Name)", family="paragraph")
        h2namestyle.addElement(
            style.ParagraphProperties(textalign='center',
                                      breakbefore='page',
                                      marginbottom="14pt"))
        h2namestyle.addElement(
            style.TextProperties(fontsize='14pt', fontweight='bold'))
        doc.styles.addElement(h2namestyle)
        styles['h2br'] = h2namestyle

        h2style = style.Style(name="Heading 2", family="paragraph")
        h2style.addElement(
            style.ParagraphProperties(textalign='center', margintop="12pt"))
        h2style.addElement(
            style.TextProperties(fontsize='14pt', fontweight='bold'))
        doc.styles.addElement(h2style)
        styles['h2'] = h2style

        h3style = style.Style(name="Heading 3", family="paragraph")
        # h3style.addElement(style.ParagraphProperties(textalign='center',
        # margintop="12pt", borderbottom="0.06pt solid #000000"))
        h3style.addElement(
            style.ParagraphProperties(textalign='center', margintop="12pt"))
        h3style.addElement(style.TextProperties(fontweight='bold'))
        doc.styles.addElement(h3style)
        styles['h3'] = h3style

        # style for image
        imgstyle = style.Style(name="ImgStyle",
                               family="graphic",
                               parentstylename="Graphics")
        imgstyle.addElement(
            style.GraphicProperties(verticalpos='top',
                                    verticalrel='paragraph-content',
                                    horizontalpos='right',
                                    horizontalrel='page-content',
                                    marginleft="0.1in",
                                    marginbottom="0.1in"))
        doc.automaticstyles.addElement(imgstyle)
        styles['img'] = imgstyle

        # centered paragraph
        centered = style.Style(name="centered", family="paragraph")
        centered.addElement(style.ParagraphProperties(textalign='center'))
        doc.styles.addElement(centered)
        styles['center'] = centered

        # centered frame
        frame_center = style.Style(name="frame-center",
                                   family="graphic",
                                   parentstylename="Graphics")
        frame_center.addElement(
            style.GraphicProperties(horizontalpos="center",
                                    horizontalrel="paragraph"))
        doc.automaticstyles.addElement(frame_center)
        styles['frame_center'] = frame_center

        # style for tree table
        treetablestyle = style.Style(name="TreeTableStyle", family="table")
        treetablestyle.addElement(style.TableProperties(align='center'))
        doc.automaticstyles.addElement(treetablestyle)
        styles['treetable'] = treetablestyle

        treecellstyle = style.Style(name="TreeTableCellStyle",
                                    family="table-cell")
        treecellstyle.addElement(
            style.TableCellProperties(verticalalign='middle',
                                      padding='0.03in'))
        doc.automaticstyles.addElement(treecellstyle)
        styles['treecell'] = treecellstyle

        treeparastyle = style.Style(name="TreeTableParaStyle",
                                    family="paragraph")
        treeparastyle.addElement(
            style.ParagraphProperties(textalign='center',
                                      verticalalign='middle',
                                      border="0.06pt solid #000000",
                                      padding='0.01in'))
        treeparastyle.addElement(style.TextProperties(fontsize='10pt'))
        doc.automaticstyles.addElement(treeparastyle)
        styles['treepara'] = treeparastyle

        return styles
#
# Contributor(s):
#
from xliff_parser import HandleXliffParsing
import sys

from odf.opendocument import OpenDocumentText
from odf import style, table
from odf.text import P, UserFieldDecls, UserFieldDecl, UserFieldInput
from odf.namespaces import TABLENS

textdoc = OpenDocumentText()
# Create a style for the table content. One we can modify
# later in the word processor.
tablecontents = style.Style(name="Table Contents", family="paragraph")
tablecontents.addElement(style.ParagraphProperties(numberlines="false", linenumber="0"))
textdoc.styles.addElement(tablecontents)

#tablestyle = style.Style(name="Table style", family="table")
#tablestyle.addElement(style.TableProperties(protected="true"))
#textdoc.automaticstyles.addElement(tablestyle)

# Create automatic styles for the column widths.
widthwide = style.Style(name="Wwide", family="table-column")
widthwide.addElement(style.TableColumnProperties(columnwidth="8cm"))
textdoc.automaticstyles.addElement(widthwide)

tcstyle = style.Style(name="Table Cell", family="table-cell")
tcstyle.addElement(style.TableCellProperties(cellprotect="protected"))
textdoc.automaticstyles.addElement(tcstyle)
 def testAttribute(self):
     """ Test that you can add a normal attributes using 'qattributes' """
     standard = style.Style(name=u"Standard", family=u"paragraph")
     p = style.ParagraphProperties(
         qattributes={(TEXTNS, u'enable-numbering'): 'true'})
     standard.addElement(p)
    doc.meta.addElement(dc.Language(text=language))
if publisher is not None:
#   doc.meta.addElement(dc.Publisher(text=publisher))
    doc.meta.addElement(meta.UserDefined(name="Publisher", text=publisher))
if copyrights is not None:
#   doc.meta.addElement(dc.Rights(text=copyrights))
    doc.meta.addElement(meta.UserDefined(name="Rights", text=copyrights))
if ebooknum is not None:
    doc.meta.addElement(meta.UserDefined(name="EText", text=ebooknum))

arial =  style.FontFace(name="Arial", fontfamily="Arial", fontfamilygeneric="swiss", fontpitch="variable")
doc.fontfacedecls.addElement(arial)

# Paragraph styles
standardstyle = style.Style(name="Standard", family="paragraph")
standardstyle.addElement(style.ParagraphProperties(marginbottom="0cm", margintop="0cm" ))
doc.styles.addElement(standardstyle)

h1style = style.Style(name="Heading 1", family="paragraph", defaultoutlinelevel="1")
h1style.addElement(style.TextProperties(attributes={'fontsize':"20pt", 'fontweight':"bold"}))
doc.styles.addElement(h1style)

textbodystyle = style.Style(name="Text body", family="paragraph", parentstylename=standardstyle)
textbodystyle.addElement(style.ParagraphProperties(attributes={'marginbottom':"0.212cm", 'margintop':"0cm",
  'textalign':"justify", 'justifysingleword':"false"}))
doc.styles.addElement(textbodystyle)

subtitlestyle = style.Style(name="Subtitle", family="paragraph", nextstylename=textbodystyle)
subtitlestyle.addElement(style.ParagraphProperties(textalign="center") )
subtitlestyle.addElement(style.TextProperties(fontsize="14pt", fontstyle="italic", fontname="Arial"))
doc.styles.addElement(subtitlestyle)