def make_tableFlowRightToLeft(): doc, section, styles = RTFTestCase.initializeDoc() section.append('Table with content flowing right to left') table = Table(TableTestCase.col4, TableTestCase.col1, TableTestCase.col2, TableTestCase.col3) table.AddRow( Cell('one'), Cell('two'), Cell('three'), Cell('This is pretty amazing', flow=Cell.FLOW_RL_TB, start_vertical_merge=True)) for i in range(10): table.AddRow(Cell('one'), Cell('two'), Cell('three'), Cell(vertical_merge=True)) section.append(table) return doc
def test_tableAddRow(self): table = Table(1000) with pytest.raises(Exception): table.AddRow() # no params at all table.AddRow(Cell('1')) # one cell, ok with pytest.raises(Exception): table.AddRow(Cell('1'), Cell('1')) # 2 cells, error with pytest.raises(Exception): table.AddRow(1) # height but no cell, error table.AddRow(1, Cell('1')) # height plus one cell, ok with pytest.raises(Exception): table.AddRow(1, Cell('1'), Cell('1')) # height with 2 cells, error
def getTable(self, **kwargs): """ @param table_left_offset (integer) @param merged_footer whether combine the content of all the footer cells into one paragraph (boolean) @param space_before_footer insert blank line before the merged footer paragraph (boolean) """ from PyRTF.document.paragraph import Paragraph, Table, Cell #from PyRTF.PropertySets import ParagraphPropertySet self._convert_table(**kwargs) col_count = self._table_elements['col.cnt'] tbl_left_offset = kwargs.get('table_left_offset', 108) ret = Table(left_offset=tbl_left_offset) tbl_layout = self._get_column_layout(col_count, **kwargs) ret.SetColumnWidths(*(tbl_layout)) if len(self._table_elements['head']) > 0: header_row = list() for a_head in self._table_elements['head'][:col_count]: head_p = Paragraph(a_head['value']) if self._head_style: head_p.Style = self._head_style rhead = Cell(head_p) header_row.append(rhead) ret.AddRow(*header_row) for row in self._table_elements['body']: single_row = list() for a_cell in row[:col_count]: cell_p = Paragraph(a_cell['value']) if self._cell_style: cell_p.Style = self._cell_style rcell = Cell(cell_p) single_row.append(rcell) ret.AddRow(*single_row) if len(self._table_elements['foot']) > 0: if kwargs.get('merged_footer', True): combined = list() combined.append(ret) if kwargs.get('space_before_footer', True): spacer_p = Paragraph('') combined.append(spacer_p) foot_p = Paragraph(self._table_elements['foot'][0]['value']) if self._foot_style: foot_p.Style = self._foot_style combined.append(foot_p) # override rvalue; ret = tuple(combined) else: foot_row = list() for a_foot in self._table_elements['foot'][:col_count]: foot_p = Paragraph(a_foot['value']) if self._foot_style: foot_p.Style = self._foot_style rfoot = Cell(foot_p) foot_row.append(rfoot) ret.AddRow(*foot_row) return ret
def make_tableHorizontalCellMerge(): doc, section, styles = RTFTestCase.initializeDoc() section.append('Table with Horizontal Cells Merged') table = Table(TableTestCase.col1, TableTestCase.col2, TableTestCase.col3) table.AddRow(Cell('A-one'), Cell('A-two'), Cell('A-three')) table.AddRow(Cell('A-one'), Cell('A-two', span=2)) table.AddRow(Cell('A-one', span=3)) table.AddRow(Cell('A-one'), Cell('A-two'), Cell('A-three')) table.AddRow(Cell('A-one', span=2), Cell('A-two')) section.append(table) return doc
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
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
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
def make_tables(): doc, section, styles = RTFTestCase.initializeDoc() p = Paragraph(styles.ParagraphStyles.Heading1) p.append('Example 3') section.append(p) # changes what is now the default style of Heading1 back to Normal p = Paragraph(styles.ParagraphStyles.Normal) p.append('Example 3 demonstrates tables, tables represent one of the ' 'harder things to control in RTF as they offer alot of ' 'flexibility in formatting and layout.') section.append(p) section.append( 'Table columns are specified in widths, the following example ' 'consists of a table with 3 columns, the first column is ' '7 tab widths wide, the next two are 3 tab widths wide. ' 'The widths chosen are arbitrary, they do not have to be ' 'multiples of tab widths.') table = Table(TabPropertySet.DEFAULT_WIDTH * 7, TabPropertySet.DEFAULT_WIDTH * 3, TabPropertySet.DEFAULT_WIDTH * 3) c1 = Cell(Paragraph('Row One, Cell One')) c2 = Cell(Paragraph('Row One, Cell Two')) c3 = Cell(Paragraph('Row One, Cell Three')) table.AddRow(c1, c2, c3) c1 = Cell(Paragraph(styles.ParagraphStyles.Heading2, 'Heading2 Style')) c2 = Cell( Paragraph(styles.ParagraphStyles.Normal, 'Back to Normal Style')) c3 = Cell(Paragraph('More Normal Style')) table.AddRow(c1, c2, c3) c1 = Cell(Paragraph(styles.ParagraphStyles.Heading2, 'Heading2 Style')) c2 = Cell( Paragraph(styles.ParagraphStyles.Normal, 'Back to Normal Style')) c3 = Cell(Paragraph('More Normal Style')) table.AddRow(c1, c2, c3) section.append(table) section.append( 'Different frames can also be specified for each cell in the table ' 'and each frame can have a different width and style for each border.' ) thin_edge = BorderPropertySet(width=20, style=BorderPropertySet.SINGLE) thick_edge = BorderPropertySet(width=80, style=BorderPropertySet.SINGLE) thin_frame = FramePropertySet(thin_edge, thin_edge, thin_edge, thin_edge) thick_frame = FramePropertySet(thick_edge, thick_edge, thick_edge, thick_edge) mixed_frame = FramePropertySet(thin_edge, thick_edge, thin_edge, thick_edge) table = Table(TabPropertySet.DEFAULT_WIDTH * 3, TabPropertySet.DEFAULT_WIDTH * 3, TabPropertySet.DEFAULT_WIDTH * 3) c1 = Cell(Paragraph('R1C1'), thin_frame) c2 = Cell(Paragraph('R1C2')) c3 = Cell(Paragraph('R1C3'), thick_frame) table.AddRow(c1, c2, c3) c1 = Cell(Paragraph('R2C1')) c2 = Cell(Paragraph('R2C2')) c3 = Cell(Paragraph('R2C3')) table.AddRow(c1, c2, c3) c1 = Cell(Paragraph('R3C1'), mixed_frame) c2 = Cell(Paragraph('R3C2')) c3 = Cell(Paragraph('R3C3'), mixed_frame) table.AddRow(c1, c2, c3) section.append(table) section.append( 'In fact frames can be applied to paragraphs too, not just cells.') p = Paragraph(styles.ParagraphStyles.Normal, thin_frame) p.append('This whole paragraph is in a frame.') section.append(p) return doc
def make_tableVerticalCellMerge(): doc, section, styles = RTFTestCase.initializeDoc() section.append('Table with Vertical Cells Merged') table = Table(TableTestCase.col1, TableTestCase.col2, TableTestCase.col3) table.AddRow(Cell('A-one'), Cell('A-two', vertical_merge=True), Cell('A-three')) table.AddRow(Cell('A-one'), Cell(vertical_merge=True), Cell('A-three')) table.AddRow(Cell('A-one'), Cell('A-two', start_vertical_merge=True), Cell('A-three')) table.AddRow(Cell('A-one'), Cell(vertical_merge=True), Cell('A-three')) table.AddRow( Cell(Paragraph( ParagraphPropertySet(alignment=ParagraphPropertySet.CENTER), 'SPREAD'), span=3)) table.AddRow(Cell('A-one'), Cell('A-two', vertical_merge=True), Cell('A-three')) table.AddRow(Cell('A-one'), Cell(vertical_merge=True), Cell('A-three')) table.AddRow(Cell('A-one'), Cell('A-two', start_vertical_merge=True), Cell('A-three')) table.AddRow(Cell('A-one'), Cell(vertical_merge=True), Cell('A-three')) section.append(table) return doc