def get_monitoring_file(self, date_items_dict, monitoring_profile=None): _file = tempfile.NamedTemporaryFile() if not date_items_dict: return _file current_date = utc_to_local(app.config['DEFAULT_TIMEZONE'], utcnow()).strftime('%d/%m/%Y') doc = Document() section = Section() ss = doc.StyleSheet p1 = Paragraph(ss.ParagraphStyles.Heading1) p1.append('{} Monitoring: {} ({})'.format(app.config.get('MONITORING_REPORT_NAME', 'Newsroom'), monitoring_profile['name'], current_date)) section.append(p1) for d in date_items_dict.keys(): date_p = Paragraph(ss.ParagraphStyles.Normal) date_p.append(LINE, d.strftime('%d/%m/%Y')) section.append(date_p) for item in date_items_dict[d]: self.format_item(item, ss, monitoring_profile, section) doc.Sections.append(section) app.customize_rtf_file(doc) doc.write(_file.name) return _file
def get_monitoring_file(self, date_items_dict, monitoring_profile=None): _file = tempfile.NamedTemporaryFile() if not date_items_dict: return _file current_date = utc_to_local(app.config['DEFAULT_TIMEZONE'], utcnow()).strftime('%d/%m/%Y') doc = Document() section = Section() ss = doc.StyleSheet general_settings = get_settings_collection().find_one( GENERAL_SETTINGS_LOOKUP) if general_settings and general_settings['values'].get( 'monitoring_report_logo_path'): image_filename = general_settings['values'].get( 'monitoring_report_logo_path') if os.path.exists(image_filename): try: image = LogoImage(general_settings['values'].get( 'monitoring_report_logo_path')) section.append(Paragraph(image)) except Exception as e: logger.exception(e) logger.error( 'Failed to open logo image {}'.format(image_filename)) else: logger.error( 'Unable to find logo image {}'.format(image_filename)) p1 = Paragraph(ss.ParagraphStyles.Heading1) p1.append('{} Monitoring: {} ({})'.format( app.config.get('MONITORING_REPORT_NAME', 'Newsroom'), monitoring_profile['name'], current_date)) section.append(p1) for d in date_items_dict.keys(): date_p = Paragraph(ss.ParagraphStyles.Normal) date_p.append(LINE, d.strftime('%d/%m/%Y')) section.append(date_p) for item in date_items_dict[d]: self.format_item(item, ss, monitoring_profile, section) doc.Sections.append(section) app.customize_rtf_file(doc) doc.write(_file.name) return _file
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
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
def write_rtf(fh, fout): """ Writes the text found in fh to rtf found in fout """ wordpad_header = textwrap.dedent(r''' {\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fnil\fcharset255 Times New Roman;} {\*\generator Riched20 10.0.14393}\viewkind4\uc1 ''').strip().replace('\n', '\r\n') center_space = ' ' r = Renderer() doc = Document() ss = doc.StyleSheet sec = Section() doc.Sections.append(sec) is_blank = False paragraph_text = [''] for line in fh: if not line or line.isspace(): is_blank = True if is_blank: # first element of paragraph_text is left-aligned, subsequent # elements are centered is_centered = False for sec_line in paragraph_text: if is_centered: para_props = ParagraphPropertySet( alignment=ParagraphPropertySet.CENTER) p = Paragraph(ss.ParagraphStyles.Normal, para_props) p.append(sec_line) sec.append(p) elif sec_line: # first element may be nothing, but not whitespace sec.append(sec_line) is_centered = True is_blank = False paragraph_text = [''] if line.startswith(center_space): paragraph_text.append(line.strip()) is_blank = True else: paragraph_text[0] += ' ' + line paragraph_text[0] = paragraph_text[0].strip() fout.write(wordpad_header) r.Write(doc, fout)
def _to_rtf(self, **kwargs): """convert internal representation of document structure into RTF stream @rtype `PyRTF.Elements.Document` """ from PyRTF.Constants import Languages from PyRTF.Elements import Document # capture all the styles; self._style_cache = self._collect_styles(**kwargs) # create document object; _doc = Document( style_sheet=self._style_cache, default_language=getattr(Languages, self.DEFAULT_LANGUAGE), ) # parse element objects and add to document; _sect = self._collect_elements(**kwargs) _doc.Sections.append(_sect) return _doc
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
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 initializeDoc(): doc = Document() section = Section() doc.Sections.append(section) return (doc, section, doc.StyleSheet)