예제 #1
0
    def make_headerFooterDiffPages():
        doc = Document()
        ss = doc.StyleSheet
        section = Section()
        doc.Sections.append(section)

        section.FirstHeader.append('This is the header for the first page.')
        section.FirstFooter.append('This is the footer for the first page.')

        section.Header.append(
            'This is the header that will appear on subsequent pages.')
        section.Footer.append(
            'This is the footer that will appear on subsequent pages.')

        p = Paragraph(ss.ParagraphStyles.Heading1)
        p.append('Example 6')
        section.append(p)

        #    blank paragraphs are just empty strings
        section.append('')

        p = Paragraph(ss.ParagraphStyles.Normal)
        p.append(
            'This document has different headers and footers for the first and then subsequent pages. '
            'If you insert a page break you should see a different header and footer.'
        )
        section.append(p)

        return doc
예제 #2
0
    def make_headerFooterSimple():
        doc = Document()
        ss = doc.StyleSheet
        section = Section()
        doc.Sections.append(section)

        section.Header.append('This is the header')
        section.Footer.append('This is the footer')

        p = Paragraph(ss.ParagraphStyles.Heading1)
        p.append('Example 5')
        section.append(p)

        #    blank paragraphs are just empty strings
        section.append('')

        p = Paragraph(ss.ParagraphStyles.Normal)
        p.append('This document has a header and a footer.')
        section.append(p)

        return doc
예제 #3
0
def basic_table_with_input(dependent_variable_name,
                           rows_statistical_significance,
                           number_of_observations, r_squared):
    #from PyRTF.Elements import Document, Section, BorderPropertySet, FramePropertySet, Table
    from PyRTF.utils import RTFTestCase
    from PyRTF.Elements import Document
    from PyRTF.document.section import Section
    from PyRTF.document.paragraph import Cell, Paragraph, Table
    #from PyRTF.Styles import TextStyle, ParagraphStyle
    from PyRTF.document.character import Text
    from PyRTF.PropertySets import BorderPropertySet, FramePropertySet, MarginsPropertySet, ParagraphPropertySet, TabPropertySet, TextPropertySet
    #
    doc = Document()
    ss = doc.StyleSheet
    section = Section()
    doc.Sections.append(section)
    #
    thin_edge = BorderPropertySet(width=20,
                                  style=BorderPropertySet.SINGLE,
                                  colour=ss.Colours.Black)
    thick_edge = BorderPropertySet(width=80, style=BorderPropertySet.SINGLE)
    zero_edge_thin = BorderPropertySet(width=20,
                                       style=BorderPropertySet.SINGLE,
                                       colour=ss.Colours.White)
    #
    thin_frame = FramePropertySet(thin_edge, thin_edge, thin_edge, thin_edge)
    thick_frame = FramePropertySet(thick_edge, thick_edge, thick_edge,
                                   thick_edge)
    thin_frame_top_only = FramePropertySet(top=thin_edge,
                                           left=None,
                                           bottom=None,
                                           right=None)
    thin_frame_bottom_only = FramePropertySet(top=None,
                                              left=None,
                                              bottom=thin_edge,
                                              right=None)
    thin_frame_top_and_bottom_only = FramePropertySet(top=thin_edge,
                                                      left=None,
                                                      bottom=thin_edge,
                                                      right=None)
    zero_frame = FramePropertySet(top=None, left=None, bottom=None, right=None)
    #
    table = Table(TabPropertySet.DEFAULT_WIDTH * 5,
                  TabPropertySet.DEFAULT_WIDTH * 3,
                  TabPropertySet.DEFAULT_WIDTH * 3)
    #
    c1 = Cell(Paragraph(''), thin_frame_top_and_bottom_only)
    c2 = Cell(Paragraph(dependent_variable_name + ' - coeff'),
              thin_frame_top_and_bottom_only)
    c3 = Cell(Paragraph(dependent_variable_name + ' - Std error'),
              thin_frame_top_and_bottom_only)
    table.AddRow(c1, c2, c3)
    #
    for row in rows_statistical_significance:
        c1 = Cell(Paragraph(row[0]), zero_frame)
        c2 = Cell(Paragraph(row[1]), zero_frame)
        c3 = Cell(Paragraph(row[2]), zero_frame)
        table.AddRow(c1, c2, c3)
    #
    #c1 = Cell(Paragraph(''), thin_frame_bottom_only)
    #c2 = Cell(Paragraph('(.013)'), thin_frame_bottom_only)
    #c3 = Cell(Paragraph('tbd'), thin_frame_bottom_only)
    #table.AddRow(c1, c2)
    #
    c1 = Cell(Paragraph('Observations'), thin_frame_top_only)
    c2 = Cell(Paragraph(number_of_observations), thin_frame_top_only)
    c3 = Cell(Paragraph(''), thin_frame_top_only)
    table.AddRow(c1, c2, c3)
    #
    tps = TextPropertySet(italic=True)
    text = Text('R^2', tps)
    #
    #c1 = Cell(Paragraph('R^2', text), thin_frame_bottom_only)
    c1 = Cell(Paragraph(text), thin_frame_bottom_only)
    c2 = Cell(Paragraph(r_squared), thin_frame_bottom_only)
    c3 = Cell(Paragraph(''), thin_frame_bottom_only)
    table.AddRow(c1, c2, c3)
    #
    section.append(table)
    #
    section.append('Standard errors in parentheses')
    section.append('* p<0.1, ** p<0.05, *** p<0.01')
    #
    return doc
예제 #4
0
def balance_table(rows, number_of_observations_1, number_of_observations_2):
    #from PyRTF.Elements import Document, Section, BorderPropertySet, FramePropertySet, Table
    from PyRTF.utils import RTFTestCase
    from PyRTF.Elements import Document
    from PyRTF.document.section import Section
    from PyRTF.document.paragraph import Cell, Paragraph, Table
    #from PyRTF.Styles import TextStyle, ParagraphStyle
    from PyRTF.document.character import Text
    from PyRTF.PropertySets import BorderPropertySet, FramePropertySet, MarginsPropertySet, ParagraphPropertySet, TabPropertySet, TextPropertySet
    #
    doc = Document()
    ss = doc.StyleSheet
    section = Section()
    doc.Sections.append(section)
    #
    thin_edge = BorderPropertySet(width=20,
                                  style=BorderPropertySet.SINGLE,
                                  colour=ss.Colours.Black)
    thick_edge = BorderPropertySet(width=80, style=BorderPropertySet.SINGLE)
    zero_edge_thin = BorderPropertySet(width=20,
                                       style=BorderPropertySet.SINGLE,
                                       colour=ss.Colours.White)
    #
    thin_frame = FramePropertySet(thin_edge, thin_edge, thin_edge, thin_edge)
    thick_frame = FramePropertySet(thick_edge, thick_edge, thick_edge,
                                   thick_edge)
    thin_frame_top_only = FramePropertySet(top=thin_edge,
                                           left=None,
                                           bottom=None,
                                           right=None)
    thin_frame_bottom_only = FramePropertySet(top=None,
                                              left=None,
                                              bottom=thin_edge,
                                              right=None)
    thin_frame_top_and_bottom_only = FramePropertySet(top=thin_edge,
                                                      left=None,
                                                      bottom=thin_edge,
                                                      right=None)
    zero_frame = FramePropertySet(top=None, left=None, bottom=None, right=None)
    #
    table = Table(TabPropertySet.DEFAULT_WIDTH * 5,
                  TabPropertySet.DEFAULT_WIDTH * 3,
                  TabPropertySet.DEFAULT_WIDTH * 3)
    #
    c1 = Cell(Paragraph(''), thin_frame_top_and_bottom_only)
    c2 = Cell(Paragraph("Republican ('Control')"),
              thin_frame_top_and_bottom_only)
    c3 = Cell(Paragraph("Democrat ('Treatment')"),
              thin_frame_top_and_bottom_only)
    table.AddRow(c1, c2, c3)
    #rows_statistical_significance_1
    c1 = Cell(Paragraph(rows[0][0]), zero_frame)
    c2 = Cell(Paragraph(rows[0][1]), zero_frame)
    c3 = Cell(Paragraph(rows[0][2]), zero_frame)
    table.AddRow(c1, c2, c3)
    #
    c1 = Cell(Paragraph(rows[1][0]), zero_frame)
    c2 = Cell(Paragraph(rows[1][1]), zero_frame)
    c3 = Cell(Paragraph(rows[1][2]), zero_frame)
    table.AddRow(c1, c2, c3)
    #
    c1 = Cell(Paragraph(rows[2][0]), zero_frame)
    c2 = Cell(Paragraph(rows[2][1]), zero_frame)
    c3 = Cell(Paragraph(rows[2][2]), zero_frame)
    table.AddRow(c1, c2, c3)
    #
    #c1 = Cell(Paragraph(''), thin_frame_bottom_only)
    #c2 = Cell(Paragraph('(.013)'), thin_frame_bottom_only)
    #c3 = Cell(Paragraph('tbd'), thin_frame_bottom_only)
    #table.AddRow(c1, c2)
    #
    c1 = Cell(Paragraph('Observations'), thin_frame_top_and_bottom_only)
    c2 = Cell(Paragraph(number_of_observations_1),
              thin_frame_top_and_bottom_only)
    c3 = Cell(Paragraph(number_of_observations_2),
              thin_frame_top_and_bottom_only)
    table.AddRow(c1, c2, c3)
    #
    section.append(table)
    #
    return doc
예제 #5
0
def basic_table():
    #from PyRTF.Elements import Document, Section, BorderPropertySet, FramePropertySet, Table
    from PyRTF.utils import RTFTestCase
    from PyRTF.Elements import Document
    from PyRTF.document.section import Section
    from PyRTF.document.paragraph import Cell, Paragraph, Table
    #from PyRTF.Styles import TextStyle, ParagraphStyle
    from PyRTF.document.character import Text
    from PyRTF.PropertySets import BorderPropertySet, FramePropertySet, MarginsPropertySet, ParagraphPropertySet, TabPropertySet, TextPropertySet
    #
    doc = Document()
    ss = doc.StyleSheet
    section = Section()
    doc.Sections.append(section)
    #
    thin_edge = BorderPropertySet(width=20,
                                  style=BorderPropertySet.SINGLE,
                                  colour=ss.Colours.Black)
    thick_edge = BorderPropertySet(width=80, style=BorderPropertySet.SINGLE)
    zero_edge_thin = BorderPropertySet(width=20,
                                       style=BorderPropertySet.SINGLE,
                                       colour=ss.Colours.White)
    #
    thin_frame = FramePropertySet(thin_edge, thin_edge, thin_edge, thin_edge)
    thick_frame = FramePropertySet(thick_edge, thick_edge, thick_edge,
                                   thick_edge)
    thin_frame_top_only = FramePropertySet(top=thin_edge,
                                           left=None,
                                           bottom=None,
                                           right=None)
    thin_frame_bottom_only = FramePropertySet(top=None,
                                              left=None,
                                              bottom=thin_edge,
                                              right=None)
    thin_frame_top_and_bottom_only = FramePropertySet(top=thin_edge,
                                                      left=None,
                                                      bottom=thin_edge,
                                                      right=None)
    #
    table = Table(TabPropertySet.DEFAULT_WIDTH * 5,
                  TabPropertySet.DEFAULT_WIDTH * 3)
    #
    c1 = Cell(Paragraph(''), thin_frame_top_and_bottom_only)
    c2 = Cell(Paragraph('Recommends AI'), thin_frame_top_and_bottom_only)
    #c3 = Cell(Paragraph('tbd'), thin_frame_top_and_bottom_only)
    table.AddRow(c1, c2)
    #
    c1 = Cell(Paragraph('Read ethics article'), thin_frame_top_only)
    c2 = Cell(Paragraph('-.38***'), thin_frame_top_only)
    #c3 = Cell(Paragraph('tbd'), thin_frame_top_only)
    table.AddRow(c1, c2)
    #
    c1 = Cell(Paragraph(''), thin_frame_bottom_only)
    c2 = Cell(Paragraph('(.013)'), thin_frame_bottom_only)
    #c3 = Cell(Paragraph('tbd'), thin_frame_bottom_only)
    table.AddRow(c1, c2)
    #
    c1 = Cell(Paragraph('Observations'), thin_frame_top_only)
    c2 = Cell(Paragraph('5000'), thin_frame_top_only)
    #c3 = Cell(Paragraph('tbd'), thin_frame_top_and_bottom_only)
    table.AddRow(c1, c2)
    #
    tps = TextPropertySet(italic=True)
    text = Text('R^2', tps)
    #
    #c1 = Cell(Paragraph('R^2', text), thin_frame_bottom_only)
    c1 = Cell(Paragraph(text), thin_frame_bottom_only)
    c2 = Cell(Paragraph('0.147'), thin_frame_bottom_only)
    #c3 = Cell(Paragraph('tbd'), thin_frame_bottom_only)
    table.AddRow(c1, c2)
    #
    section.append(table)
    #
    section.append('Standard errors in parentheses')
    section.append('* p<0.1, ** p<0.05, *** p<0.01')
    #
    return doc
예제 #6
0
    def _collect_elements(self, **kwargs):
        """get all the elements

        @rtype `PyRTF.document.section.Section`
        """
        from PyRTF.document.section import Section
        from .utils import RPar, RTable, RList

        def _inject_blankline(element, **kwargs):
            line = None
            line_text = kwargs.get('alt.line.text', '')
            if element.get(self.KEY_ADD_NEWLINE, False):
                line = RPar(line_text,
                            style=self._default_p_style).getParagraph(**kwargs)
            return line

        # go through element list and add to section;
        ret = Section()
        for a_element in self._element_cache:
            e_type = a_element.get(self.KEY_TYPE, None)
            e_ctx = a_element.get(self.KEY_VALUE, '')
            e_style = a_element.get(self.KEY_STYLE, None)

            # use captured styles to create document element;
            element_obj = None
            if e_type == self.ELEMENT_PARAGRAPH:
                style_obj = self._style_cache.ParagraphStyles.get_by_name(
                    e_style)
                element_obj = RPar(e_ctx,
                                   style=style_obj).getParagraph(**kwargs)
            elif e_type == self.ELEMENT_PARTIAL:
                style_obj = self._style_cache.ParagraphStyles.get_by_name(
                    e_style)
                rp = RPar(None, style=style_obj)
                rp.append(*e_ctx)
                element_obj = rp.getParagraph(**kwargs)
            elif e_type == self.ELEMENT_TABLE:
                cell_s_obj = self._style_cache.ParagraphStyles.get_by_name(
                    e_style)
                head_s_obj = self._style_cache.ParagraphStyles.get_by_name(
                    self._get_bold_style_name(cell_s_obj.name))
                element_obj = RTable(
                    e_ctx, style=cell_s_obj,
                    header_style=head_s_obj).getTable(**kwargs)
            elif e_type == self.ELEMENT_LIST:
                fallback_style_obj = self._style_cache.ParagraphStyles.get_by_name(
                    self.DEFAULT_LIST_STYLE_NAME)
                # try to use 'e_style', and fall back to default style;
                style_obj = self._style_cache.ParagraphStyles.get_by_name(
                    e_style, fallback_style_obj)
                element_obj = RList(e_ctx, style=style_obj).getList(**kwargs)
            else:
                pass
            # push the element object to cache;
            if element_obj:
                if e_type == self.ELEMENT_LIST or (e_type == self.ELEMENT_TABLE
                                                   and isinstance(
                                                       element_obj, tuple)):
                    ret.extend(element_obj)
                else:
                    ret.append(element_obj)
                # optional blank line;
                trailing = _inject_blankline(a_element, **kwargs)
                if trailing:
                    ret.append(trailing)
        return ret
예제 #7
0
 def NewSection(self, *params, **kwargs):
     result = Section(*params, **kwargs)
     self.Sections.append(result)
     return result
예제 #8
0
 def __init__(self, doc=None, *args, **kwargs):
     Section.__init__(self, *args, **kwargs)
     self.doc = doc or Document(self.defaultStyle())
     self.doc.Sections.append(self)
예제 #9
0
    def make_headerFooterDiffPagesAndSections():
        doc = Document()
        ss = doc.StyleSheet
        section = Section()
        doc.Sections.append(section)

        section.FirstHeader.append('This is the header for the first page.')
        section.FirstFooter.append('This is the footer for the first page.')

        section.Header.append(
            'This is the header that will appear on subsequent pages.')
        section.Footer.append(
            'This is the footer that will appear on subsequent pages.')

        p = Paragraph(ss.ParagraphStyles.Heading1)
        p.append('Example 7')
        section.append(p)

        p = Paragraph(ss.ParagraphStyles.Normal)
        p.append(
            'This document has different headers and footers for the first and then subsequent pages. '
            'If you insert a page break you should see a different header and footer.'
        )
        section.append(p)

        p = Paragraph(ss.ParagraphStyles.Normal,
                      ParagraphPropertySet().SetPageBreakBefore(True))
        p.append('This should be page 2 '
                 'with the subsequent headers and footers.')
        section.append(p)

        section = Section(break_type=Section.PAGE, first_page_number=1)
        doc.Sections.append(section)

        section.FirstHeader.append(
            'This is the header for the first page of the second section.')
        section.FirstFooter.append(
            'This is the footer for the first page of the second section.')

        section.Header.append(
            'This is the header that will appear on subsequent pages for the second section.'
        )
        p = Paragraph(
            'This is the footer that will appear on subsequent pages for the second section.',
            LINE)
        p.append(PAGE_NUMBER, ' of ', SECTION_PAGES)
        section.Footer.append(p)

        section.append('This is the first page')

        p = Paragraph(ParagraphPropertySet().SetPageBreakBefore(True),
                      'This is the second page')
        section.append(p)

        return doc