Exemple #1
0
 def test_insert_paragraph(self):
     body = self.body.clone()
     paragraph = odf_create_paragraph(u'An inserted test',
                                      style='Text_20_body')
     body.append(paragraph)
     last_paragraph = body.get_paragraphs()[-1]
     self.assertEqual(last_paragraph.get_text(), u'An inserted test')
 def test_insert_paragraph(self):
     body = self.body.clone()
     paragraph = odf_create_paragraph(u'An inserted test',
                                      style='Text_20_body')
     body.append(paragraph)
     last_paragraph = body.get_paragraphs()[-1]
     self.assertEqual(last_paragraph.get_text(), u'An inserted test')
Exemple #3
0
 def test_create_note1(self):
     # With an odf_element
     note_body = odf_create_paragraph(u'a footnote', style='Standard')
     note = odf_create_note(note_id='note1', citation=u'1', body=note_body)
     expected = self.expected.replace(
         '<text:p>', '<text:p text:style-name="Standard">')
     self.assertEqual(note.serialize(), expected)
Exemple #4
0
 def test_get_draw_orphans_connector(self):
     body = self.content.get_body()
     page = body.get_draw_page()
     orphan_connector = odf_create_connector()
     orphan_connector.append(odf_create_paragraph(u'Orphan c'))
     body.append(orphan_connector)
     connectors = body.get_orphan_draw_connectors()
     self.assertEqual(len(connectors), 1)
Exemple #5
0
 def test_create_note1(self):
     # With an odf_element
     note_body = odf_create_paragraph(u'a footnote', style='Standard')
     note = odf_create_note(note_id='note1', citation=u'1',
                            body=note_body)
     expected = self.expected.replace('<text:p>',
                                      '<text:p text:style-name="Standard">')
     self.assertEqual(note.serialize(), expected)
 def test_get_draw_orphans_connector(self):
     body = self.content.get_body()
     page = body.get_draw_page_by_position(0)
     orphan_connector = odf_create_connector()
     orphan_connector.append_element(odf_create_paragraph(u'Orphan c'))
     body.append_element(orphan_connector)
     connectors = body.get_draw_orphans_connectors()
     self.assertEqual(len(connectors), 1)
 def test_append_plain_text_guess_iso(self):
     txt = 'A test,\n   \twith \n\n some \xe9 and \t and     5 spaces.'
     para = odf_create_paragraph()
     para.append_plain_text(txt)
     expected = ('<text:p>A test,<text:line-break/> <text:s text:c="2"/>'
                 '<text:tab/>with <text:line-break/><text:line-break/> '
                 'some &#233; and <text:tab/> and <text:s text:c="4"/>'
                 '5 spaces.</text:p>')
     self.assertEqual(para.serialize(), expected)
 def test_text(self):
     text = u"Le Père Noël a une moustache rouge."
     paragraph = odf_create_paragraph(text)
     paragraph.set_span(u"highlight", regex=u"rouge")
     expected = ('<text:p>Le P&#232;re No&#235;l a une moustache '
                   '<text:span '
                     'text:style-name="highlight">rouge</text:span>.'
                 '</text:p>')
     self.assertEqual(paragraph.serialize(), expected)
Exemple #9
0
 def test_text(self):
     text = u"Le Père Noël a une moustache rouge."
     paragraph = odf_create_paragraph(text)
     paragraph.set_span(u"highlight", regex=u"rouge")
     expected = ('<text:p>Le P&#232;re No&#235;l a une moustache '
                   '<text:span '
                     'text:style-name="highlight">rouge</text:span>.'
                 '</text:p>')
     self.assertEqual(paragraph.serialize(), expected)
Exemple #10
0
 def test_offset(self):
     text = u"Le Père Noël a une moustache rouge."
     paragraph = odf_create_paragraph(text)
     paragraph.set_span(u"highlight", offset=text.index(u"moustache"))
     expected = ('<text:p>Le P&#232;re No&#235;l a une '
                   '<text:span text:style-name="highlight">moustache '
                   'rouge.</text:span>'
                 '</text:p>')
     self.assertEqual(paragraph.serialize(), expected)
 def test_offset(self):
     text = u"Le Père Noël a une moustache rouge."
     paragraph = odf_create_paragraph(text)
     paragraph.set_span(u"highlight", offset=text.index(u"moustache"))
     expected = ('<text:p>Le P&#232;re No&#235;l a une '
                   '<text:span text:style-name="highlight">moustache '
                   'rouge.</text:span>'
                 '</text:p>')
     self.assertEqual(paragraph.serialize(), expected)
 def test_append_plain_text_guess_iso(self):
     txt = 'A test,\n   \twith \n\n some \xe9 and \t and     5 spaces.'
     para = odf_create_paragraph()
     para.append_plain_text(txt)
     expected = ('<text:p>A test,<text:line-break/> <text:s text:c="2"/>'
                 '<text:tab/>with <text:line-break/><text:line-break/> '
                 'some &#233; and <text:tab/> and <text:s text:c="4"/>'
                 '5 spaces.</text:p>')
     self.assertEqual(para.serialize(), expected)
Exemple #13
0
 def test_set_bookmark_with_limits(self):
     paragraph = odf_create_paragraph(u"aa bb bb aa")
     paragraph.set_bookmark("bookmark", position=(6, 8))
     expected = ('<text:p>aa bb '
                 '<text:bookmark-start text:name="bookmark"/>'
                 'bb'
                 '<text:bookmark-end text:name="bookmark"/>'
                 ' aa'
                 '</text:p>')
     self.assertEqual(paragraph.serialize(), expected)
Exemple #14
0
 def test_set_bookmark_with_limits(self):
     paragraph = odf_create_paragraph(u"aa bb bb aa")
     paragraph.set_bookmark("bookmark", position=(6, 8))
     expected = ('<text:p>aa bb '
                   '<text:bookmark-start text:name="bookmark"/>'
                   'bb'
                   '<text:bookmark-end text:name="bookmark"/>'
                   ' aa'
                 '</text:p>')
     self.assertEqual(paragraph.serialize(), expected)
Exemple #15
0
 def test_set_bookmark_with_role(self):
     paragraph = odf_create_paragraph(u"aa")
     paragraph.set_bookmark("bookmark", role="start")
     paragraph.set_bookmark("bookmark", role="end", position=-1)
     expected = ('<text:p>'
                   '<text:bookmark-start text:name="bookmark"/>'
                   'aa'
                   '<text:bookmark-end text:name="bookmark"/>'
                 '</text:p>')
     self.assertEqual(paragraph.serialize(), expected)
Exemple #16
0
 def test_set_bookmark_with_role(self):
     paragraph = odf_create_paragraph(u"aa")
     paragraph.set_bookmark("bookmark", role="start")
     paragraph.set_bookmark("bookmark", role="end", position=-1)
     expected = ('<text:p>'
                 '<text:bookmark-start text:name="bookmark"/>'
                 'aa'
                 '<text:bookmark-end text:name="bookmark"/>'
                 '</text:p>')
     self.assertEqual(paragraph.serialize(), expected)
Exemple #17
0
    def block_code(self, code, language=None):

        if language == 'Comment':
            para = odf_create_paragraph(style=u'md2odp-NoteText')
            notes = odf_create_element('presentation:notes')

            # no lang given, use plain monospace formatting
            for elem in handle_whitespace(code):
                if isinstance(elem, basestring):
                    span = odf_create_element('text:span')
                    span.set_text(elem)
                    para.append(span)
                else:
                    para.append(elem)

            frame = odf_create_text_frame(
                para,
                presentation_style=u'md2odp-OutlineText',
                size=(u'18cm', u'25cm'),
                position=(u'2cm', u'2cm'),
                presentation_class=u'title')
            notes.append(frame)
            return ODFPartialTree.from_metrics_provider([notes], self)

        elif language is not None:
            # explicit lang given, use syntax highlighting
            para = odf_create_paragraph(style=u'md2odp-ParagraphCodeStyle')
            lexer = get_lexer_by_name(language)

            for span in self.formatter.format(lexer.get_tokens(code)):
                para.append(span)
        else:
            # no lang given, use plain monospace formatting
            para = odf_create_paragraph(style=u'md2odp-ParagraphCodeStyle')
            for elem in handle_whitespace(code):
                if isinstance(elem, basestring):
                    span = odf_create_element('text:span')
                    span.set_text(elem)
                    para.append(span)
                else:
                    para.append(elem)

        return ODFPartialTree.from_metrics_provider([para], self)
Exemple #18
0
 def test_set_bookmark_with_after_without_position(self):
     paragraph = odf_create_paragraph(u"aa bb aa aa cc aa dd")
     paragraph.set_span(style="style", regex="bb aa aa")
     paragraph.set_span(style="style", regex="dd")
     paragraph.set_bookmark("bookmark", after="aa")
     expected = ('<text:p>aa<text:bookmark text:name="bookmark"/> '
                   '<text:span text:style-name="style">bb aa aa'
                   '</text:span>'
                 ' cc aa <text:span text:style-name="style">dd</text:span>'
                 '</text:p>')
     self.assertEqual(paragraph.serialize(), expected)
Exemple #19
0
 def test_set_bookmark_with_after_without_position(self):
     paragraph = odf_create_paragraph(u"aa bb aa aa cc aa dd")
     paragraph.set_span(style="style", regex="bb aa aa")
     paragraph.set_span(style="style", regex="dd")
     paragraph.set_bookmark("bookmark", after="aa")
     expected = ('<text:p>aa<text:bookmark text:name="bookmark"/> '
                 '<text:span text:style-name="style">bb aa aa'
                 '</text:span>'
                 ' cc aa <text:span text:style-name="style">dd</text:span>'
                 '</text:p>')
     self.assertEqual(paragraph.serialize(), expected)
 def test_set_bookmark_with_limits(self):
     paragraph = odf_create_paragraph(u"aa bb bb aa")
     paragraph.set_bookmark("bookmark", limits=(6, 8))
     expected = (
         "<text:p>aa bb "
         '<text:bookmark-start text:name="bookmark"/>'
         "bb"
         '<text:bookmark-end text:name="bookmark"/>'
         " aa"
         "</text:p>"
     )
     self.assertEqual(paragraph.serialize(), expected)
 def test_set_bookmark_with_content(self):
     paragraph = odf_create_paragraph(u"aa bb bb aa")
     paragraph.set_bookmark("bookmark", content="bb", position=1)
     expected = (
         "<text:p>aa bb "
         '<text:bookmark-start text:name="bookmark"/>'
         "bb"
         '<text:bookmark-end text:name="bookmark"/>'
         " aa"
         "</text:p>"
     )
     self.assertEqual(paragraph.serialize(), expected)
 def test_set_bookmark_with_before(self):
     paragraph = odf_create_paragraph(u"aa bb aa aa cc aa dd")
     paragraph.set_span(style="style", regex="bb aa aa")
     paragraph.set_span(style="style", regex="dd")
     paragraph.set_bookmark("bookmark", before="aa", position=1)
     expected = (
         "<text:p>aa "
         '<text:span text:style-name="style">bb '
         '<text:bookmark text:name="bookmark"/>aa aa'
         "</text:span>"
         ' cc aa <text:span text:style-name="style">dd</text:span>'
         "</text:p>"
     )
     self.assertEqual(paragraph.serialize(), expected)
def convert_paragraph(node, context):
    paragraph = odf_create_paragraph()
    context["top"].append_element(paragraph)

    # Save the current top
    old_top = context["top"]

    # Convert
    context["top"] = paragraph
    for child in node:
        convert_node(child, context)

    # And restore the top
    context["top"] = old_top
Exemple #24
0
 def test_set_bookmark_with_position(self):
     paragraph = odf_create_paragraph(u"aa bb aa aa cc aa dd")
     paragraph.set_span(style="style", regex="bb aa aa")
     paragraph.set_span(style="style", regex="dd")
     paragraph.set_bookmark("bookmark1", position=0)
     paragraph.set_bookmark("bookmark2", position=2)
     paragraph.set_bookmark("bookmark3",
                            position=len(u"aa bb aa aa cc aa dd"))
     expected = ('<text:p><text:bookmark text:name="bookmark1"/>aa'
                   '<text:bookmark text:name="bookmark2"/> '
                   '<text:span text:style-name="style">bb aa aa</text:span>'
                   ' cc aa <text:span text:style-name="style">dd'
                   '<text:bookmark text:name="bookmark3"/></text:span>'
                 '</text:p>')
     self.assertEqual(paragraph.serialize(), expected)
 def test_set_bookmark_with_end(self):
     paragraph = odf_create_paragraph("aa bb aa aa cc aa dd")
     paragraph.set_span(style="style", regex="bb aa aa")
     paragraph.set_span(style="style", regex="dd")
     paragraph.set_bookmark("bookmark1", after="cc", position=-1)
     paragraph.set_bookmark("bookmark2", position=-1)
     expected = ('<text:p>aa '
                 '<text:span text:style-name="style">'
                 'bb aa aa'
                 '</text:span>'
                 ' cc<text:bookmark text:name="bookmark1"/> aa '
                 '<text:span text:style-name="style">dd</text:span>'
                 '<text:bookmark text:name="bookmark2"/>'
                 '</text:p>')
     self.assertEqual(paragraph.serialize(), expected)
Exemple #26
0
 def test_set_bookmark_with_position(self):
     paragraph = odf_create_paragraph(u"aa bb aa aa cc aa dd")
     paragraph.set_span(style="style", regex="bb aa aa")
     paragraph.set_span(style="style", regex="dd")
     paragraph.set_bookmark("bookmark1", position=0)
     paragraph.set_bookmark("bookmark2", position=2)
     paragraph.set_bookmark("bookmark3",
                            position=len(u"aa bb aa aa cc aa dd"))
     expected = ('<text:p><text:bookmark text:name="bookmark1"/>aa'
                 '<text:bookmark text:name="bookmark2"/> '
                 '<text:span text:style-name="style">bb aa aa</text:span>'
                 ' cc aa <text:span text:style-name="style">dd'
                 '<text:bookmark text:name="bookmark3"/></text:span>'
                 '</text:p>')
     self.assertEqual(paragraph.serialize(), expected)
Exemple #27
0
 def test_insert_annotation(self):
     text = u"It's like you're in a cave."
     creator = u"Plato"
     date = datetime(2009, 8, 19)
     annotation = odf_create_annotation(text, creator=creator, date=date)
     paragraph = odf_create_paragraph(u"Un paragraphe")
     paragraph.insert_annotation(annotation, after=u"para")
     expected = ('<text:p>Un para'
                   '<office:annotation>'
                     '<text:p>It\'s like you\'re in a cave.</text:p>'
                     '<dc:creator>Plato</dc:creator>'
                     '<dc:date>2009-08-19T00:00:00</dc:date>'
                   '</office:annotation>'
                 'graphe</text:p>')
     self.assertEqual(paragraph.serialize(), expected)
Exemple #28
0
 def test_insert_note(self):
     note = odf_create_note(note_id='note1', citation=u"1",
                            body=u"élément pertubateur")
     paragraph = odf_create_paragraph(u"Un paragraphe")
     paragraph.insert_note(note, after=u"para")
     expected = ('<text:p>Un para'
                   '<text:note text:note-class="footnote" '
                     'text:id="note1">'
                     '<text:note-citation>1</text:note-citation>'
                     '<text:note-body>'
                       '<text:p>&#233;l&#233;ment pertubateur</text:p>'
                     '</text:note-body>'
                   '</text:note>'
                 'graphe</text:p>')
     self.assertEqual(paragraph.serialize(), expected)
Exemple #29
0
 def test_insert_note(self):
     note = odf_create_note(note_id='note1', citation=u"1",
                            body=u"élément pertubateur")
     paragraph = odf_create_paragraph(u"Un paragraphe")
     paragraph.insert_note(note, after=u"para")
     expected = ('<text:p>Un para'
                   '<text:note text:note-class="footnote" '
                     'text:id="note1">'
                     '<text:note-citation>1</text:note-citation>'
                     '<text:note-body>'
                       '<text:p>&#233;l&#233;ment pertubateur</text:p>'
                     '</text:note-body>'
                   '</text:note>'
                 'graphe</text:p>')
     self.assertEqual(paragraph.serialize(), expected)
Exemple #30
0
 def test_insert_annotation(self):
     text = u"It's like you're in a cave."
     creator = u"Plato"
     date = datetime(2009, 8, 19)
     annotation = odf_create_annotation(text, creator=creator, date=date)
     paragraph = odf_create_paragraph(u"Un paragraphe")
     paragraph.insert_annotation(annotation, after=u"para")
     expected = ('<text:p>Un para'
                   '<office:annotation>'
                     '<text:p>It\'s like you\'re in a cave.</text:p>'
                     '<dc:creator>Plato</dc:creator>'
                     '<dc:date>2009-08-19T00:00:00</dc:date>'
                   '</office:annotation>'
                 'graphe</text:p>')
     self.assertEqual(paragraph.serialize(), expected)
 def test_set_bookmark_with_end(self):
     paragraph = odf_create_paragraph(u"aa bb aa aa cc aa dd")
     paragraph.set_span(style="style", regex="bb aa aa")
     paragraph.set_span(style="style", regex="dd")
     paragraph.set_bookmark("bookmark1", after="cc", position=-1)
     paragraph.set_bookmark("bookmark2", position=-1)
     expected = (
         "<text:p>aa "
         '<text:span text:style-name="style">'
         "bb aa aa"
         "</text:span>"
         ' cc<text:bookmark text:name="bookmark1"/> aa '
         '<text:span text:style-name="style">dd</text:span>'
         '<text:bookmark text:name="bookmark2"/>'
         "</text:p>"
     )
     self.assertEqual(paragraph.serialize(), expected)
Exemple #32
0
def spreadsheet_to_text(indoc, outdoc):
    inbody = indoc.get_body()
    outbody = outdoc.get_body()
    # Copy tables
    for intable in inbody.get_tables():
        # clone/rstrip the table
        clone = intable.clone()
        clone.rstrip(aggressive=True)
        # Skip empty table
        if clone.get_size() == (0, 0):
            continue
        # At least OOo Writer doesn't like formulas referencing merged
        # cells, so expand
        outtable = odf_create_table(clone.get_name(),
                style=clone.get_style())
        # Columns
        for column in clone.traverse_columns():
            outtable.append(column)
        # Rows
        for inrow in clone.traverse():
            outrow = odf_create_row(style=inrow.get_style())
            # Cells
            for cell in inrow.traverse():
                # Formula
                formula = cell.get_formula()
                if formula is not None:
                    if formula.startswith('oooc:'):
                        formula = oooc_to_ooow(formula)
                    else:
                        # Found an OpenFormula test case
                        raise NotImplementedError, formula
                    cell.set_formula(formula)
                outrow.append(cell)
            outtable.append_row(outrow)
        outbody.append(outtable)
        # Separate tables with an empty line
        outbody.append(odf_create_paragraph())
    # Copy styles
    for family in ('table', 'table-column', 'table-row', 'table-cell'):
        for style in indoc.get_styles(family=family):
            automatic = (style.get_parent().get_tag()
                    == 'office:automatic-styles')
            default = style.get_tag() == 'style:default-style'
            outdoc.insert_style(style, automatic=automatic,
                    default=default)
def convert_literal_block(node, context):
    paragraph = odf_create_paragraph(style="Preformatted_20_Text")
    context["top"].append_element(paragraph)

    # Convert
    for child in node:
        # Only text
        if child.tagname != "#text":
            warn('node "%s" not supported in literal block' % child.tagname)
            continue
        text = child.astext()

        tmp = []
        spaces = 0
        for c in text:
            if c == '\n':
                if tmp:
                    tmp = u"".join(tmp)
                    paragraph.append_element(tmp)
                    tmp = []
                spaces = 0
                paragraph.append_element(odf_create_line_break())
            elif c == '\r':
                continue
            elif c == ' ':
                spaces += 1
            elif c == '\t':
                # Tab = 4 spaces
                spaces += 4
            else:
                if spaces >= 2:
                    if tmp:
                        tmp = u"".join(tmp)
                        paragraph.append_element(tmp)
                        tmp = []
                    paragraph.append_element(
                              odf_create_undividable_space(spaces))
                    spaces = 0
                elif spaces == 1:
                    tmp.append(' ')
                    spaces = 0
                tmp.append(c)
        if tmp:
            tmp = u"".join(tmp)
            paragraph.append_element(tmp)
Exemple #34
0
def wrap_spans(odf_elements):
    """For any homogeneous toplevel range of text:span elements, wrap them
       into a paragraph"""
    res = []
    para = None
    for elem in odf_elements:
        if isinstance(elem, odf_span) or isinstance(elem, odf_link):
            if para is None:
                para = odf_create_paragraph()
            para.append(elem)
        else:
            if para is not None:
                res.append(para)
                para = None
            res.append(elem)
    if para is not None:
        res.append(para)
    return res
def convert_definition_list(node, context):
    # TODO Add the style
    for item in node:
        if item.tagname != "definition_list_item":
            warn('node "%s" not supported in definition_list' % item.tagname)
            continue

        for child in item:
            tagname = child.tagname
            if tagname == "term":
                paragraph = odf_create_paragraph(text=child.astext())
                context["top"].append_element(paragraph)
            elif tagname == "definition":
                for subchildren in child:
                    convert_node(subchildren, context)
            else:
                warn('node "%s" not supported in definition_list_item' %
                        item.tagname)
Exemple #36
0
def wrap_spans(odf_elements):
    """For any homogeneous toplevel range of text:span elements, wrap them
       into a paragraph"""
    res = []
    para = None
    for elem in odf_elements:
        if isinstance(elem, odf_span) or isinstance(elem, odf_link):
            if para is None:
                para = odf_create_paragraph()
            para.append(elem)
        else:
            if para is not None:
                res.append(para)
                para = None
            res.append(elem)
    if para is not None:
        res.append(para)
    return res
Exemple #37
0
    def block_quote(self, text):
        para = odf_create_paragraph(style=u'md2odp-ParagraphQuoteStyle')
        span = odf_create_element('text:span')
        span.set_text(u'“')
        para.append(span)

        span = odf_create_element('text:span')
        for elem in text.get():
            span.append(elem)
        para.append(span)

        span = odf_create_element('text:span')
        span.set_text(u'”')
        para.append(span)

        # pylint: disable=maybe-no-member
        para.set_span(u'md2odp-TextQuoteStyle', regex=u'“')
        para.set_span(u'md2odp-TextQuoteStyle', regex=u'”')
        return ODFPartialTree.from_metrics_provider([para], self)
Exemple #38
0
    def block_quote(self, text):
        para = odf_create_paragraph(style=u'md2odp-ParagraphQuoteStyle')
        span = odf_create_element('text:span')
        span.set_text(u'“')
        para.append(span)

        span = odf_create_element('text:span')
        for elem in text.get():
            span.append(elem)
        para.append(span)

        span = odf_create_element('text:span')
        span.set_text(u'”')
        para.append(span)

        # pylint: disable=maybe-no-member
        para.set_span(u'md2odp-TextQuoteStyle', regex=u'“')
        para.set_span(u'md2odp-TextQuoteStyle', regex=u'”')
        return ODFPartialTree.from_metrics_provider([para], self)
Exemple #39
0
    def block_code(self, code, language=None):
        para = odf_create_paragraph(style=u'md2odp-ParagraphCodeStyle')

        if language is not None:
            # explicit lang given, use syntax highlighting
            lexer = get_lexer_by_name(language)

            for span in self.formatter.format(lexer.get_tokens(code)):
                para.append(span)
        else:
            # no lang given, use plain monospace formatting
            for elem in handle_whitespace(code):
                if isinstance(elem, basestring):
                    span = odf_create_element('text:span')
                    span.set_text(elem)
                    para.append(span)
                else:
                    para.append(elem)

        return ODFPartialTree.from_metrics_provider([para], self)
Exemple #40
0
    def block_code(self, code, language=None):
        para = odf_create_paragraph(style=u'md2odp-ParagraphCodeStyle')

        if language is not None:
            # explicit lang given, use syntax highlighting
            lexer = get_lexer_by_name(language)

            for span in self.formatter.format(lexer.get_tokens(code)):
                para.append(span)
        else:
            # no lang given, use plain monospace formatting
            for elem in handle_whitespace(code):
                if isinstance(elem, basestring):
                    span = odf_create_element('text:span')
                    span.set_text(elem)
                    para.append(span)
                else:
                    para.append(elem)

        return ODFPartialTree.from_metrics_provider([para], self)
Exemple #41
0
# remove default styles
document.delete_styles()
# add our styles
document.insert_style(_style_font_1, default=True)
document.insert_style(_style_font_2, default=True)
document.insert_style(_style_font_3, default=True)
document.insert_style(_style_page, automatic=True)
document.insert_style(_style_master)
document.insert_style(_style_footer)
document.insert_style(_style_description)
document.insert_style(_style_small_serif)
document.insert_style(_style_bold)

body = document.get_body()

paragraph = odf_create_paragraph(u'', style=u'description')
text = text_1.decode('utf-8')
paragraph.append_plain_text(text)  # expect Unicode
body.append(paragraph)

paragraph = odf_create_paragraph(style=u'line')
body.append(paragraph)

paragraph = odf_create_paragraph(style=u'smallserif')
paragraph.append_plain_text(text_2,
                            encoding='utf-8')  # can decode from encoding
body.append(paragraph)

paragraph = odf_create_paragraph(style=u'line')
body.append(paragraph)
# -*- coding: UTF-8 -*-
# Let's add another section to make our document clear:
# Annotations are notes that don’t appear in the document but typically on a
# side bar in a desktop application. So they are not printed.
from lpod.document import odf_new_document
from lpod.paragraph import odf_create_paragraph

from lpod.heading import odf_create_heading

document = odf_new_document('text')
body = document.get_body()

body.append(odf_create_heading(1, u"Annotations"))
paragraph = odf_create_paragraph(
    u"A paragraph with an annotation in the middle.")
body.append(paragraph)

# Annotations are inserted like notes but they are simpler:

paragraph.insert_annotation(after=u"annotation",
                            body=u"It's so easy!",
                            creator=u"Luis")

# Annotation arguments are quite different:
# after   =>  The word after what the annotation is inserted.
# body    =>  The annotation itself, at the end of the page.
# creator =>  The author of the annotation.
# date    =>  A datetime value, by default datetime.now().
Exemple #43
0
# Hello messages
print 'lpod installation test'
print ' Version           : %s' % __version__
print ' Installation path : %s' % __installation_path__
print
print 'Generating test_output/use_case2.odt ...'

# Go
document = odf_new_document('text')
body = document.get_body()

# 1- The image
# ------------
image = Image.open('samples/image.png')
width, height = image.size
paragraph = odf_create_paragraph(style=u"Standard")
# 72 ppp
frame = odf_create_frame('frame1', 'Graphics',
                         str(width / 72.0) + 'in',
                         str(height / 72.0) + 'in')
internal_name = 'Pictures/image.png'
image = odf_create_image(internal_name)
frame.append(image)
paragraph.append(frame)
body.append(paragraph)

# And store the data
container = document.container
container.set_part(internal_name, open('samples/image.png').read())

# 2- Congratulations (=> style on paragraph)
Exemple #44
0
# -*- coding: UTF-8 -*-
from lpod.document import odf_new_document
from lpod.heading import odf_create_heading
from lpod.paragraph import odf_create_paragraph

document = odf_new_document('text')
body = document.get_body()

# Let's add another section to make our document clear:

body.append(odf_create_heading(1, u"Tables"))
body.append(odf_create_paragraph(u"A 3x3 table:"))

# Creating a table :

from lpod.table import odf_create_table
table = odf_create_table(u"Table 1", width=3, height=3)
body.append(table)
Exemple #45
0
def create_paragraph(text, style=None):

    return odf_create_paragraph(text, style=style)
Exemple #46
0
# use merge_styles_from to copy default style from some document
#
path = normpath(join(lpod.__file__, '../templates/lpod_styles.odt'))
doc_style = odf_get_document(path)
document.merge_styles_from(doc_style)

#
# Pages, header and footer
#

# Automatic style to set the master page
style = odf_create_style('paragraph', master_page=u"First_20_Page")
document.insert_style(style, automatic=True)

# The first paragraph will set the page::
paragraph = odf_create_paragraph(u"lpOD generated Document with styled pages",
                                 style.get_style_name())
body.append(paragraph)

# To modify the footer and header we get the style
first_page_style = document.get_style('master-page', u"First_20_Page")

# Overwrite the footer
first_page_style.set_footer(u'lpOD project')

# Complement the header
header = first_page_style.get_header()
header.append(odf_create_paragraph(u"Final Version"))

# Example of default style: a date
date_style = odf_create_default_date_style()
document.insert_style(date_style, automatic=True)
# -*- coding: UTF-8 -*-
#  I want to write "Hello World" on the first page.

from lpod.document import odf_new_document
from lpod.paragraph import odf_create_paragraph

document = odf_new_document('text')
body = document.get_body()
paragraph = odf_create_paragraph(u"Hello World")
body.append(paragraph)
Exemple #48
0
# -*- coding: UTF-8 -*-
from lpod.document import odf_new_document
from lpod.paragraph import odf_create_paragraph

document = odf_new_document('text')
body = document.get_body()

paragraph = odf_create_paragraph(u"A paragraph with a footnote "
        u"about references in it.")
body.append(paragraph)

# Notes are quite complex so they deserve a dedicated API on paragraphs:

paragraph.insert_note(after=u"graph", note_id='note1', citation=u"1",
        body=(u'Author, A. (2007). "How to cite references", '
              u'New York: McGraw-Hill.'))

# That looks complex so we detail the arguments:
#
# after    =>   The word after what the “¹” citation is inserted.
# note_id  =>	The unique identifier of the note in the document.
# citation => 	The symbol the user sees to follow the footnote.
# body 	   =>   The footnote itself, at the end of the page.
#
# LpOD creates footnotes by default. To create endnotes (notes
# that appear at the end of the document), give the
# note_class=’endnote’ parameter.
Exemple #49
0
    first_cell.set_cell_value(u"Hello")

    # Modified cells must be pushed back
    # Could be pushed to another position
    first_row.set_cell(0, first_cell)

    # Modified rows must be pushed back
    # Could be pushed to another position
    table.set_row(0, first_row)

    # Accessing cells from the table
    second_cell = table.get_cell("B1")

    # Cells are XML elements
    second_cell.clear()
    second_cell.append(odf_create_paragraph(u"World"))

    # Modified cells must be pushed back
    # Could be pushed to another position
    table.set_cell((1, 0), second_cell)

    # Append a column (and adjust the table size)
    table.append_column(odf_create_column())

    # Add an image in the document
    image_uri = document.add_file('../.static/banner-lpod_en.png')

    # Images are in frame
    frame = odf_create_image_frame(image_uri, size=('11.87cm', '1.75cm'),
            position=('0cm', '0cm'))
# Imports from lpod
from lpod.document import odf_new_document
from lpod.heading import odf_create_heading
from lpod.paragraph import odf_create_paragraph

# Create the document
my_document = odf_new_document('text')
body = my_document.get_body()

# Add content (See Create_a_basic_document.py)
title1 = odf_create_heading(1, random_text(1)[:70])
body.append(title1)
for p in range(3):
    title = odf_create_heading(2, random_text(1)[:70])
    body.append(title)
    paragraph = odf_create_paragraph(random_text(10))

    # Adding Annotation
    # Annotations are notes that don’t appear in the document but
    # typically on a side bar in a desktop application. So they are not printed.

    # Now we add some annotation on each paragraph
    some_word = paragraph.get_text(True).split()[3]
    # choosing the 4th word of the paragraph to insert the note

    paragraph.insert_annotation(
        after=some_word,  # The word after what the annotation is inserted.
        body=u"It's so easy!",  # The annotation itself, at the end of the page.
        creator=u"Bob"  # The author of the annotation.
        # date= xxx              A datetime value, by default datetime.now().
    )
            'fo:border': u"0cm solid #000000"})
        context['doc'].insert_style(caption_style, automatic=True)
        styles['caption_style'] = caption_style

    # Frame style for the image frame
    image_style = styles.get('image_style')
    if image_style is None:
        image_style = odf_create_style('graphic', parent="Graphics")
        image_style.set_style_properties({'style:horizontal-pos': u"center",
            'style:horizontal-rel': u"paragraph"})
        context['doc'].insert_style(image_style, automatic=True)
        styles['image_style'] = image_style

    # In text application, image must be inserted in a paragraph
    if context["top"].get_tagname() == "office:text":
        container = odf_create_paragraph()
        context["top"].append_element(container)
    else:
        container = context["top"]

    if caption:
        paragraph = odf_create_paragraph()
        image_frame = odf_create_image_frame(local_uri, size=size,
                style=image_style.get_style_name())
        paragraph.append_element(image_frame)
        paragraph.append_element(caption)
        # A new frame, we fix only the width
        text_frame = odf_create_text_frame(paragraph, size=(size[0], None),
                style=caption_style.get_style_name())
        container.append_element(text_frame)
    else:
Exemple #52
0
body = document.get_body()

for numero, filename in enumerate(listdir('samples')):
    # Heading
    heading = odf_create_heading(1, text=unicode(filename, 'utf-8'))
    body.append(heading)
    path = join('samples', filename)
    mimetype, _ = guess_type(path)
    if mimetype is None:
        mimetype = 'application/octet-stream'
    if mimetype.startswith('image/'):
        # Add the image
        internal_name = 'Pictures/' + filename
        image = Image.open(path)
        width, height = image.size
        paragraph = odf_create_paragraph('Standard')
        # 72 ppp
        frame = odf_create_frame('frame_%d' % numero, 'Graphics',
                                 str(width / 72.0) + 'in',
                                 str(height / 72.0) + 'in')
        image = odf_create_image(internal_name)
        frame.append(image)
        paragraph.append(frame)
        body.append(paragraph)

        # And store the data
        container = document.container
        container.set_part(internal_name, open(path).read())
    elif mimetype in ('text/csv', 'text/comma-separated-values'):
        table = odf_create_table(u"table %d" % numero, style=u"Standard")
        csv = reader(open(path))
Exemple #53
0
# Hello messages
print 'lpod installation test'
print ' Version           : %s' %  __version__
print ' Installation path : %s' % __installation_path__
print
print 'Generating test_output/use_case2.odt ...'

# Go
document = odf_new_document('text')
body = document.get_body()

# 0- The image
# ------------
image = Image.open('samples/image.png')
width, height = image.size
paragraph = odf_create_paragraph(style=u"Standard")
# 72 ppp
frame = odf_create_frame('frame1', 'Graphics',
                         str(width / 72.0) + 'in',
                         str(height / 72.0) + 'in')
internal_name = 'Pictures/image.png'
image = odf_create_image(internal_name)
frame.append(image)
paragraph.append(frame)
body.append(paragraph)

# And store the data
container = document.container
container.set_part(internal_name, open('samples/image.png').read())

document = odf_new_document_from_type('text')
body = document.get_body()

samples = vfs.open('samples')
for numero, filename in enumerate(samples.get_names()):
    # Heading
    heading = odf_create_heading(2, text=unicode(filename, 'utf-8'))
    body.append_element(heading)

    uri = samples.get_uri(filename)
    handler = get_handler(uri)
    if isinstance(handler, Image):
        # Add the image
        internal_name = 'Pictures/' + filename
        width, height = handler.get_size()
        paragraph = odf_create_paragraph('Standard')
        # 72 ppp
        frame = odf_create_frame('frame_%d' % numero, 'Graphics',
                                 str(width / 72.0) + 'in',
                                 str(height / 72.0) + 'in')
        image = odf_create_image(internal_name)
        frame.append_element(image)
        paragraph.append_element(frame)
        body.append_element(paragraph)

        # And store the data
        container = document.container
        container.set_part(internal_name,
                           samples.open(filename).read())
    elif isinstance(handler, CSVFile):
        table = odf_create_table(u"table %d" % numero, style=u"Standard")
        cat_list.append(Product(chr(65 + p), price))
        price += 10.5
    return cat_list

tax_rate = .196

if __name__=="__main__":
    commercial = odf_new_document('text')
    body = commercial.get_body()
    catalog = make_catalog()

    title1 = odf_create_heading(1, u"Basic commercial document")
    body.append(title1)
    title11 = odf_create_heading(2, u"Available products")
    body.append(title11)
    paragraph = odf_create_paragraph(u"Here the list:")
    body.append(paragraph)

    # List of products in a list :
    product_list = odf_create_list()
    body.append(product_list)
    for product in catalog:
        item = odf_create_list_item(u"%-10s, price: %.2f €" % (
                                            product.name, product.price))
        product_list.append(item)

    title12 = odf_create_heading(2, u"Your command")
    body.append(title12)

    command = {0 : 1, 1 : 12, 2 : 3, 4 : 5 }
Exemple #56
0
from lpod.list import odf_create_list, odf_create_list_item
from lpod.note import odf_create_note, odf_create_annotation
from lpod.paragraph import odf_create_paragraph
from lpod.table import odf_create_table
from lpod.toc import odf_create_toc

# Creation of the document
document = odf_new_document_from_type('text')
body = document.get_body()

# Add (empty) Table of content
toc = odf_create_toc()
body.append(toc)

# Add Paragraph
paragraph = odf_create_paragraph(u'lpOD generated Document')
body.append(paragraph)

# Add Heading
heading = odf_create_heading(1, text=u'Lists')
body.append(heading)

#
# A list
#
my_list = odf_create_list([u'chocolat', u'café'])

item = odf_create_list_item(u'Du thé')
item.append(odf_create_list([u'thé vert', u'thé rouge']))
my_list.append_item(item)