def test_table_cell_border_top(self): style = odf_create_style('table-cell', border_top="0.002cm") self.assertEqual(style.serialize(), ('<style:style ' 'style:family="table-cell"><style:table-cell-properties ' 'fo:border-top="0.002cm" fo:border-left="none" ' 'fo:border-right="none" fo:border-bottom="none"/>' '</style:style>'))
def add_style(document, style_family, style_name, properties, parent=None): """Insert global style into given document""" style = odf_create_style(style_family, style_name, style_name, parent) for elem in properties: # pylint: disable=maybe-no-member style.set_properties(properties=elem[1], area=elem[0]) document.insert_style(style, automatic=True)
def test_table_column_width(self): style = odf_create_style('table-column', width="5cm") self.assertEqual( style.serialize(), ('<style:style ' 'style:family="table-column"><style:table-column-properties ' 'style:column-width="5cm"/></style:style>'))
def test_table_row_height(self): style = odf_create_style('table-row', height="5cm") self.assertEqual( style.serialize(), ('<style:style ' 'style:family="table-row"><style:table-row-properties ' 'style:row-height="5cm"/></style:style>'))
def test_table_cell_border(self): style = odf_create_style('table-cell', border="0.002cm") self.assertEqual( style.serialize(), ('<style:style ' 'style:family="table-cell"><style:table-cell-properties ' 'fo:border="0.002cm"/></style:style>'))
def test_insert_master_page_style(self): doc = self.doc style = odf_create_style('master-page', 'MyPageStyle') doc.insert_style(style) inserted_style = doc.get_style('master-page', 'MyPageStyle') self.assertEqual(style.serialize(), inserted_style.serialize())
def test_table_cell_shadow(self): style = odf_create_style('table-cell', shadow="#808080 0.176cm 0.176cm") self.assertEqual( style.serialize(), ('<style:style ' 'style:family="table-cell"><style:table-cell-properties ' 'style:shadow="#808080 0.176cm 0.176cm"/></style:style>'))
def test_insert_master_page_style(self): doc = self.doc style = odf_create_style("master-page", u"MyPageStyle") doc.insert_style(style) inserted_style = doc.get_style("master-page", u"MyPageStyle") self.assertEqual(style.serialize(), inserted_style.serialize())
def test_insert_common_style(self): doc = self.doc style = odf_create_style("paragraph", u"MyStyle") doc.insert_style(style) inserted_style = doc.get_style("paragraph", u"MyStyle") self.assertEqual(style.serialize(), inserted_style.serialize())
def test_insert_common_style(self): doc = self.doc style = odf_create_style('paragraph', 'MyStyle') doc.insert_style(style) inserted_style = doc.get_style('paragraph', 'MyStyle') self.assertEqual(style.serialize(), inserted_style.serialize())
def test_insert_with_error(self): doc = self.doc style = odf_create_style('paragraph', u'MyStyle') self.assertRaises(AttributeError, doc.insert_style, style=style, automatic=True, default=True)
def setup_document_styles(document): """ Setup all styles used in the document, after the setup the style can be used by its 'name' as X.set_style(stylename) example: cell.set_style(STYLE[TABLE_CELL_BASE]) """ doc_title_style = odf_create_style('paragraph', size='18', bold=True) STYLES[DOCUMENT_TITLE] = document.insert_style(style=doc_title_style, default=True) # Setup base cell style border = make_table_cell_border_string( thick='0.03cm', color='black' ) style = { 'color': 'black', 'background_color': (255, 255, 255), 'border_right': border, 'border_left': border, 'border_bottom': border, 'border_top': border } base_style = odf_create_table_cell_style(**style) STYLES[TABLE_CELL_BASE] = document.insert_style( style=base_style, automatic=True ) # Setup colored cell styles (based on score) for color_val, color_rgb in COLORS.items(): style['background_color'] = color_rgb _style = odf_create_table_cell_style(**style) _stylename = "{}{}".format(TABLE_CELL_AS_VALUE, color_val) STYLES[_stylename] = document.insert_style( style=_style, automatic=True ) col_style = odf_create_style('table-column', width='3cm') STYLES[DESCR_COLUMN_WIDTH] = document.insert_style( style=col_style, automatic=True )
def test_insert_default_style(self): doc = self.doc style = odf_create_style("paragraph", u"MyStyle") doc.insert_style(style, default=True) inserted_style = doc.get_style("paragraph") expected = '<style:default-style style:family="paragraph"/>' self.assertEqual(inserted_style.serialize(), expected)
def test_insert_default_style(self): doc = self.doc style = odf_create_style('paragraph', 'MyStyle') doc.insert_style(style, default=True) inserted_style = doc.get_style('paragraph') expected = '<style:default-style style:family="paragraph"/>' self.assertEqual(inserted_style.serialize(), expected)
def convert_emphasis(node, context): # Yet an emphasis style ? styles = context["styles"] if "emphasis" in styles: emphasis = styles["emphasis"] else: emphasis = odf_create_style("text", italic=True) styles["emphasis"] = emphasis context["doc"].insert_style(emphasis, automatic=True) # Convert _convert_style_like(node, context, emphasis.get_style_name())
def convert_strong(node, context): # Yet a strong style ? styles = context["styles"] if "strong" in styles: strong = styles["strong"] else: strong = odf_create_style("text", bold=True) styles["strong"] = strong context["doc"].insert_style(strong, automatic=True) # Convert _convert_style_like(node, context, strong.get_style_name())
def test_insert_style(self): content = self.content.clone() style = odf_create_style('paragraph', 'style1', area='text', **{'fo:color': '#0000ff', 'fo:background-color': '#ff0000'}) auto_styles = content.get_element('//office:automatic-styles') auto_styles.append(style) expected = ('<style:style style:name="style1" ' 'style:family="paragraph">' '<style:text-properties fo:color="#0000ff" ' 'fo:background-color="#ff0000"/>' '</style:style>') get1 = content.get_style('paragraph', u'style1') self.assertEqual(get1.serialize(), expected)
def highlight(odf_file_url, pattern, color=None, background_color=None, italic=False, bold=False, target=None, pretty=True): # Make display_name and name display_name = [u"Highlight"] if color and color != 'none': display_name.append(unicode(color).capitalize()) if background_color and background_color != 'none': display_name.append(unicode(background_color).capitalize()) if italic: display_name.append(u"Italic") if bold: display_name.append(u"Bold") display_name = u" ".join(display_name) name = display_name.replace(u" ", u"_20_") # Is our style already installed? style = document.get_style('text', name) if style is None: color = rgb2hex(color) if color != 'none' else None background_color = (rgb2hex(background_color) if background_color != 'none' else None) style = odf_create_style('text', name, italic=italic, bold=bold, color=color, background_color=background_color) document.insert_style(style, automatic=True) # Patch! body = document.get_body() i = -1 for i, paragraph in enumerate( body.get_paragraphs(content=pattern) + body.get_headings(content=pattern)): # Don't colour the table of content if paragraph.get_parent().get_tag() in ('text:index-title', 'text:index-body'): continue paragraph.set_span(name, regex=pattern) document.save(target=target, pretty=pretty) printinfo((i + 1), "paragraphs changed (0 error, 0 warning).")
def test_insert_style(self): styles = self.styles.clone() style = odf_create_style('paragraph', 'style1', area='text', **{'fo:color': '#0000ff', 'fo:background-color': '#ff0000'}) context = styles.get_element('//office:styles') context.append(style) expected = ('<style:style style:name="style1" ' 'style:family="paragraph">\n' ' <style:text-properties fo:color="#0000ff" ' 'fo:background-color="#ff0000"/>\n' '</style:style>\n') get1 = styles.get_style('paragraph', 'style1') self.assertEqual(get1.serialize(pretty=True), expected)
def action_export(self, resource, context, form): root = context.root results = context.root.search(format=self.export_resource.class_id) if not len(results): context.message = ERR_NO_DATA return context.query['batch_start'] = context.query['batch_size'] = 0 # Create ODS header_style = odf_create_style('table-cell', area='text', bold=True) document = odf_new_document_from_type('spreadsheet') document.insert_style(header_style, automatic=True) body = document.get_body() table = odf_create_table(u'Table') # Add the header row = odf_create_row() line = self.get_header(resource, context) row.set_values(line, style=header_style) table.append_row(row) # Fill content for item_brain in results.get_documents(): item_resource = root.get_resource(item_brain.abspath) line = self.get_row(resource, context, item_resource) row = odf_create_row() try: row.set_values(line) except Exception: context.message = ERROR(u'Error on line %s' % line) return table.append_row(row) body.append(table) # Extport as ODS f = StringIO() document.save(f) content = f.getvalue() f.close() # Return ODS context.set_content_type( 'application/vnd.oasis.opendocument.spreadsheet') context.set_content_disposition('attachment', self.get_filename(resource)) return content
class ODSWriter(object): mimetype = ODF_SPREADSHEET extension = 'ods' header_style = odf_create_style('table-cell', area='text', bold=True) def __init__(self, name): self.document = document = odf_new_document('spreadsheet') self.table = table = odf_create_table(name) document.get_body().append(table) document.insert_style(self.header_style, automatic=True) def add_row(self, values, is_header=False): if is_header is True: style = self.header_style else: style = None row = odf_create_row() row.set_values(values, style=style) self.table.append_row(row) def to_str(self): body = StringIO() self.document.save(body) return body.getvalue()
def test_create_style_properties_shorcut(self): style = odf_create_style('paragraph', area='text', color='#ff0000') expected = ('<style:style style:family="paragraph">' '<style:text-properties fo:color="#ff0000"/>' '</style:style>') self.assertEqual(style.serialize(), expected)
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) # ------------------------------------------ heading = odf_create_heading(1, text=u'Congratulations !') body.append(heading) # The style style = odf_create_style('paragraph', u"style1", parent=u"Standard", area='text', color=rgb2hex('blue'), background_color=rgb2hex('red')) document.insert_style(style) # The paragraph text = u'This document has been generated by the lpOD installation test.' paragraph = odf_create_paragraph(text, style=u"style1") body.append(paragraph) # 3- Your environment (=> a table) # -------------------------------- heading = odf_create_heading(1, text=u'Your environment') body.append(heading) data = []
def test_create_style_outline(self): style = odf_create_style('outline') expected = ('<text:outline-style/>') self.assertEqual(style.serialize(), expected)
def test_create_style_page_layout(self): style = odf_create_style('page-layout') expected = ('<style:page-layout/>') self.assertEqual(style.serialize(), expected)
standard = document.get_style('graphic', u"standard") standard.set_style_properties({'draw:fill-color': '#ffffff'}) # # Work on pages and add textframes # page1 = odf_create_draw_page('page1', name=u"Page 1") body.append(page1) # # Text Frame # # Set the frame color colored = odf_create_style('graphic', name=u"colored", display_name=u"Colored", parent="standard") colored.set_style_properties({'draw:fill-color': "#ad7fa8"}, area='graphic') colored.set_style_properties(color="#ffffff", area='text') document.insert_style(colored) # A paragraph style with big font big = odf_create_style('paragraph', u"big", area='paragraph', align="center") big.set_style_properties(area='text', size="32pt") document.insert_style(big, automatic=True) # Set a text frame text_frame = odf_create_text_frame([u"lpOD", u"Presentation", u"Cookbook"], size=('7cm', '5cm'), position=('11cm', '8cm'), style=u"colored",
def test_create_style_master_page(self): style = odf_create_style('master-page') expected = ('<style:master-page/>') self.assertEqual(style.serialize(), expected)
def __init__(self, document, code_font_name, break_master=None, breakheader_size=None, breakheader_position=None, content_master=None, header_size=None, header_position=None, outline_size=None, outline_position=None, highlight_style='colorful', aspect_ratio='43', autofit_text=True): mistune.Renderer.__init__(self) self.formatter = ODFFormatter(style=highlight_style) self.document = document self.doc_manifest = document.get_part(ODF_MANIFEST) # make sure nested odpdown calls don't end up writing # similarly-named images self.image_entry_id = len([ path for path in self.doc_manifest.get_paths() if path.startswith(ODFRenderer.image_prefix) ]) self.break_master = 'Default' if break_master is None else break_master self.breakheader_size = ((u'20cm', u'3cm') if breakheader_size is None else breakheader_size) self.breakheader_position = ((u'2cm', u'8cm') if breakheader_position is None else breakheader_position) self.content_master = ('Default' if content_master is None else content_master) self.header_size = ((u'20cm', u'3cm') if header_size is None else header_size) self.header_position = ((u'2cm', u'0.5cm') if header_position is None else header_position) self.outline_size = ((u'22cm', u'12cm') if outline_size is None else outline_size) self.outline_position = ((u'2cm', u'4cm') if outline_position is None else outline_position) self.aspect_ratio = aspect_ratio # font/char styles self.document.insert_style(odf_create_style( 'font-face', name=code_font_name, font_name=code_font_name, font_family=code_font_name, font_family_generic=u'modern', font_pitch=u'fixed'), automatic=True) add_style(document, 'text', u'md2odp-TextEmphasisStyle', [('text', { 'font_style': u'italic' })]) add_style(document, 'text', u'md2odp-TextDoubleEmphasisStyle', [('text', { 'font_weight': u'bold' })]) add_style( document, 'text', u'md2odp-TextQuoteStyle', # TODO: font size increase does not work currently # Bug in Impress: # schema has his - for _all_ occurences # <attribute name="fo:font-size"> # <choice> # <ref name="positiveLength"/> # <ref name="percent"/> # </choice> # </attribute> [('text', { 'size': u'200%', 'color': u'#ccf4c6' })]) add_style(document, 'text', u'md2odp-TextCodeStyle', [('text', { 'style:font_name': code_font_name })]) # paragraph styles add_style(document, 'paragraph', u'md2odp-ParagraphQuoteStyle', [('text', { 'color': u'#18a303' }), ('paragraph', { 'margin_left': u'0.5cm', 'margin_right': u'0.5cm', 'margin_top': u'0.6cm', 'margin_bottom': u'0.5cm', 'text_indent': u'-0.6cm' })]) add_style(document, 'paragraph', u'md2odp-ParagraphCodeStyle', [('text', { 'style:font_name': code_font_name }), ('paragraph', { 'margin_left': u'0.5cm', 'margin_right': u'0.5cm', 'margin_top': u'0.6cm', 'margin_bottom': u'0.6cm', 'text_indent': u'0cm' })]) add_style(document, 'paragraph', u'md2odp-NoteText', [('text', { 'color': u'#000000' })]) # graphic styles add_style(document, 'graphic', u'md2odp-ImageStyle', [('graphic', { 'stroke': u'none', 'fille': u'none', 'draw:textarea_horizontal_align': u'right', 'draw:textarea-vertical-align': u'bottom' })]) # presentation styles add_style(document, 'presentation', u'md2odp-OutlineText', ([('graphic', { 'draw:fit_to_size': u'shrink-to-fit' })] if autofit_text else [('graphic', { 'draw:auto_grow_height': u'true' })]), self.content_master + '-outline1') add_style(document, 'presentation', u'md2odp-TitleText', [('graphic', { 'draw:auto_grow_height': u'true' })], self.content_master + '-title') add_style(document, 'presentation', u'md2odp-BreakTitleText', [('graphic', { 'draw:auto_grow_height': u'true' })], self.break_master + '-title') # clone list style out of content master page (an abomination # this is not referenceable out of the presentation style...) content_master_styles = [ i for i in self.document.get_part(ODF_STYLES).get_elements( 'descendant::style:style') if (i.get_attribute('style:name') == self.content_master + '-outline1') ] if len(content_master_styles): # now stick that under custom name into automatic style section list_style = content_master_styles[0].get_elements( 'style:graphic-properties/text:list-style[1]')[0].clone() list_style.set_attribute('style:name', u'OutlineListStyle') document.insert_style(list_style, automatic=True) else: print 'WARNING: no outline list style found for ' \ 'master page "%s"!' % self.content_master # delegate to pygments formatter for their styles self.formatter.add_style_defs(document)
def test_create_style_parent(self): style = odf_create_style('paragraph', parent=u"Heading 1") expected = ('<style:style style:family="paragraph" ' 'style:parent-style-name="Heading 1"/>') self.assertEqual(style.serialize(), expected)
def test_create_style_display_name(self): style = odf_create_style('paragraph', display_name=u"Heading 1") expected = ('<style:style style:family="paragraph" ' 'style:display-name="Heading 1"/>') self.assertEqual(style.serialize(), expected)
color='white', ) style = odf_create_table_cell_style( color='grey', background_color=cell_value, border_right=border_rl, border_left=border_rl, border_bottom=border_bt, border_top=border_bt, ) name = document.insert_style(style=style, automatic=True) cell = odf_create_cell(value=rgb2hex(cell_value), style=name) row.append_cell(cell) table.append_row(row) row_style = odf_create_style('table-row', height='1.80cm') name_style_row = document.insert_style(style=row_style, automatic=True) for row in table.get_rows(): row.set_style(name_style_row) table.set_row(row.y, row) col_style = odf_create_style('table-column', width='3.6cm') name = document.insert_style(style=col_style, automatic=True) for column in table.get_columns(): column.set_style(col_style) table.set_column(column.x, column) body.append(table) if not exists('test_output'): mkdir('test_output')
def test_create_style_properties(self): style = odf_create_style('paragraph', **{'fo:margin-top': "0cm"}) expected = ('<style:style style:family="paragraph">' '<style:paragraph-properties fo:margin-top="0cm"/>' '</style:style>') self.assertEqual(style.serialize(), expected)
def setUp(self): self.style = odf_create_style('paragraph')
def test_bad_family(self): style = odf_create_style('master-page') self.assertRaises(TypeError, style.set_background)
def setUp(self): self.style = odf_create_style('list')