def odf_create_toc_source( title=None, outline_level=10, title_style=u"Contents_20_Heading", entry_style=u"Contents_20_%d" ): element = odf_create_element("text:table-of-content-source") element.set_outline_level(outline_level) if title: title_template = odf_create_element("text:index-title-template") if title_style: # This style is in the template document title_template.set_style(title_style) title_template.set_text(title) element.append(title_template) for level in range(1, 11): template = odf_create_element( """<text:table-of-content-entry-template text:outline-level="%d"> <text:index-entry-chapter/> <text:index-entry-text/> <text:index-entry-tab-stop style:type="right" style:leader-char="."/> <text:index-entry-page-number/> </text:table-of-content-entry-template>""" % level ) if entry_style: template.set_style(entry_style % level) element.append(template) return element
def odf_create_toc_source(title=None, outline_level=10, title_style=u"Contents_20_Heading", entry_style=u"Contents_20_%d"): element = odf_create_element('text:table-of-content-source') element.set_outline_level(outline_level) if title: title_template = odf_create_element('text:index-title-template') if title_style: # This style is in the template document title_template.set_style(title_style) title_template.set_text(title) element.append(title_template) for level in range(1, 11): template = odf_create_element('''<text:table-of-content-entry-template text:outline-level="%d"> <text:index-entry-chapter/> <text:index-entry-text/> <text:index-entry-tab-stop style:type="right" style:leader-char="."/> <text:index-entry-page-number/> </text:table-of-content-entry-template>''' % level) if entry_style: template.set_style(entry_style % level) element.append(template) return element
def odf_create_toc_level_style(level): """Generate an automatic default style for the given TOC level. """ tab_stop = base_tab_stop.clone() position = 17.5 - (0.5 * level) tab_stop.set_attribute("style:position", u"%dcm" % position) tab_stops = odf_create_element("style:tab-stops") tab_stops.append(tab_stop) properties = odf_create_element("style:paragraph-properties") properties.append(tab_stops) element = odf_create_style("paragraph", name=TOC_ENTRY_STYLE_PATTERN % level, parent="Contents_20_%d" % level) element.append(properties) return element
def odf_create_toc_level_style(level): """Generate an automatic default style for the given TOC level. """ tab_stop = base_tab_stop.clone() position = 17.5 - (0.5 * level) tab_stop.set_attribute('style:position', u'%dcm' % position) tab_stops = odf_create_element('style:tab-stops') tab_stops.append(tab_stop) properties = odf_create_element('style:paragraph-properties') properties.append(tab_stops) element = odf_create_style('paragraph', name=TOC_ENTRY_STYLE_PATTERN % level, parent='Contents_20_%d' % level) element.append(properties) return element
def odf_create_toc_level_style(level): """Generate an automatic default style for the given TOC level. """ tab_stop = base_tab_stop.clone() position = 17.5 - (0.5 * level) tab_stop.set_attribute('style:position', u'%dcm' % position) tab_stops = odf_create_element('<style:tab-stops/>') tab_stops.append_element(tab_stop) properties = odf_create_element('<style:paragraph-properties/>') properties.append_element(tab_stops) element = odf_create_style('paragraph', name=TOC_ENTRY_STYLE_PATTERN % level, parent='Contents_20_%d' % level) element.append_element(properties) return element
def odf_create_creation_date_variable(fixed=False, data_style=None): element = odf_create_element("text:creation-date") if fixed: element.set_attribute("text:fixed", "true") if data_style is not None: element.set_style_attribute("style:data-style-name", data_style) return element
def _odf_create_shape(type, style=None, text_style=None, shape_id=None, layer=None): """Create a shape element. Arguments: type -- str style -- unicode text_style -- unicode shape_id -- unicode layer -- str Return: odf_element """ element = odf_create_element(type) if style: element.set_style(style) if text_style: element.set_text_style(text_style) if shape_id: element.set_id(shape_id) if layer: element.set_layer(layer) return element
def set_user_defined_metadata(self, name, value): if type(value) is bool: value_type = 'boolean' value = u'true' if value else u'false' elif isinstance(value, (int, float, Decimal)): value_type = 'float' value = unicode(value) elif type(value) is date: value_type = 'date' value = unicode(Date.encode(value)) elif type(value) is datetime: value_type = 'date' value = unicode(DateTime.encode(value)) elif type(value) is str: value_type = 'string' value = unicode(value) elif type(value) is unicode: value_type = 'string' elif type(value) is timedelta: value_type = 'time' value = unicode(Duration.encode(value)) else: raise TypeError, 'unexpected type "%s" for value' % type(value) # Already the same element ? for metadata in self.get_elements('//meta:user-defined'): if metadata.get_attribute('meta:name') == name: break else: metadata = odf_create_element('meta:user-defined') metadata.set_attribute('meta:name', name) self.get_meta_body().append(metadata) metadata.set_attribute('meta:value-type', value_type) metadata.set_text(value)
def odf_create_draw_page(page_id=None, name=None, master_page=None, presentation_page_layout=None, style=None): """This element is a container for content in a drawing or presentation document. Arguments: page_id -- str name -- unicode master_page -- str presentation_page_layout -- str style -- str Return: odf_element """ element = odf_create_element('draw:page') if page_id: element.set_id(page_id) if name: element.set_name(name) if style: element.set_style(style) if master_page: element.set_master_page(master_page) if presentation_page_layout: element.set_presentation_page_layout(presentation_page_layout) return element
def _odf_create_shape(type, style=None, text_style=None, shape_id=None, layer=None): """Create a shape element. Arguments: type -- str style -- unicode text_style -- unicode shape_id -- unicode layer -- str Return: odf_element """ element = odf_create_element(type) if style: element.set_attribute('draw:style-name', style) if text_style: element.set_attribute('draw:text-style-name', text_style) if shape_id: element.set_attribute('draw:id', shape_id) if layer: element.set_attribute('draw:layer', layer) return element
def odf_create_link(href, name=None, title=None, target_frame=None, style=None, visited_style=None): """Return a text:a odf_element. Arguments: href -- string (an URI) name -- unicode title -- unicode target_name -- '_self', '_blank', '_parent', '_top' style -- string visited_style -- string """ element = odf_create_element('<text:a/>') element.set_attribute('xlink:href', href) if name is not None: element.set_attribute('office:name', name) if title is not None: element.set_attribute('office:title', title) if target_frame is not None: element.set_attribute('office:target-frame-name', target_frame) if target_frame == '_blank': element.set_attribute('xlink:show', 'new') else: element.set_attribute('xlink:show', 'replace') if style is not None: element.set_text_style(style) if visited_style is not None: element.set_attribute('text:visited-style-name', visited_style) return element
def set_properties(self, properties={}, style=None, area=None, **kw): """Set the properties of the "area" type of this style. Properties are given either as a dict or as named arguments (or both). The area is identical to the style family by default. If the properties element is missing, it is created. Instead of properties, you can pass a style with properties of the same area. These will be copied. Arguments: properties -- dict style -- odf_style area -- 'paragraph', 'text'... """ if area is None: area = self.get_family() element = self.get_element('style:%s-properties' % area) if element is None: element = odf_create_element('style:%s-properties' % area) self.append(element) if properties or kw: properties = _expand_properties(_merge_dicts(properties, kw)) elif style is not None: properties = style.get_properties(area=area) if properties is None: return for key, value in properties.iteritems(): if value is None: element.del_attribute(key) else: element.set_attribute(key, value)
def odf_create_draw_page(page_id, name=None, master_page=None, page_layout=None, style=None): """This element is a container for content in a drawing or presentation document. Arguments: page_id -- str name -- unicode master_page -- str page_layout -- str style -- str Return: odf_element """ element = odf_create_element('draw:page') element.set_page_id(page_id) if name: element.set_page_name(name) if style: element.set_attribute('draw:style-name', style) if master_page: element.set_attribute('draw:master-page-name', master_page) if page_layout: element.set_attribute('presentation:presentation-page-layout-name', page_layout) return element
def odf_create_line_break(): """ This element represents a line break. Return odf_element """ return odf_create_element('text:line-break')
def odf_create_default_number_style(): return odf_create_element( """<number:number-style style:name="lpod-default-number-style"> <number:number number:decimal-places="2" number:min-integer-digits="1"/> </number:number-style>""" )
def odf_create_note(note_class='footnote', note_id=None, citation=None, body=None): """Create either a footnote or a endnote element with the given text, optionally referencing it using the given note_id. Arguments: note_class -- 'footnote' or 'endnote' note_id -- str citation -- unicode body -- unicode or odf_element Return: odf_element """ data = ('<text:note>' '<text:note-citation/>' '<text:note-body/>' '</text:note>') element = odf_create_element(data) element.set_class(note_class) if note_id is not None: element.set_id(note_id) if citation is not None: element.set_citation(citation) if body is not None: element.set_body(body) return element
def odf_create_frame(name=None, draw_id=None, style=None, position=None, size=('1cm', '1cm'), z_index=0, presentation_class=None, anchor_type=None, page_number=None, layer=None, presentation_style=None): """Create a frame element of the given size. Position is relative to the context the frame is inserted in. If positioned by page, give the page number and the x, y position. Size is a (width, height) tuple and position is a (left, top) tuple; items are strings including the unit, e.g. ('10cm', '15cm'). Frames are not useful by themselves. You should consider calling odf_create_image_frame or odf_create_text_frame directly. Arguments: name -- unicode draw_id -- unicode style -- unicode position -- (str, str) size -- (str, str) z_index -- int (default 0) presentation_class -- unicode anchor_type -- 'page', 'frame', 'paragraph', 'char' or 'as-char' page_number (anchor_type='page') -- int layer -- unicode presentation_style -- unicode Return: odf_frame """ frame = odf_create_element('draw:frame') frame.set_size(size) frame.set_z_index(z_index) if name: frame.set_name(name) if page_number: frame.set_anchor_type('page', page_number=page_number) if anchor_type: frame.set_anchor_type(anchor_type) if position is not None: frame.set_position(position) if layer is not None: frame.set_layer(layer) if presentation_class is not None: frame.set_presentation_class(presentation_class) if style is not None: frame.set_style(style) if presentation_style is not None: frame.set_presentation_style(presentation_style) return frame
def get_deleted(self, as_text=False, no_header=False): """Get the deleted informations stored in the <text:deletion>. If as_text is True: returns the text content. If no_header is True: existing text:h are changed in text:p Arguments: as_text -- boolean no_header -- boolean Return: odf_paragraph and odf_header list """ children = self.get_children() inner = [e for e in children if e.get_tag() != 'office:change-info'] if no_header: # crude replace t:h by t:p new_inner = [] for element in inner: if element.get_tag() == 'text:h': children = element.get_children() text = element.get_text() para = odf_create_element('text:p') para.set_text(text) for c in children: para.append(c) new_inner.append(para) else: new_inner.append(element) inner = new_inner if as_text: return '\n'.join( [e.get_formatted_text(context=None) for e in inner]) else: return inner
def odf_create_anim_transitionFilter(smil_dur=None, smil_type=None, smil_subtype=None, smil_direction=None, smil_fadeColor=None, smil_mode=None): """ Used to make a beautiful transition between two frames. Arguments: smil_dur -- XXX complete me smil_type and smil_subtype -- see http://www.w3.org/TR/SMIL20/smil-transitions.html#TransitionEffects-Appendix to get a list of all types/subtypes smil_direction -- forward, reverse smil_fadeColor -- forward, reverse smil_mode -- in, out """ element = odf_create_element('anim:transitionFilter') if smil_dur: element.set_attribute('smil:dur', smil_dur) if smil_type: element.set_attribute('smil:type', smil_type) if smil_subtype: element.set_attribute('smil:subtype', smil_subtype) if smil_direction: element.set_attribute('smil:direction', smil_direction) if smil_fadeColor: element.set_attribute('smil:fadeColor', smil_fadeColor) if smil_mode: element.set_attribute('smil:mode', smil_mode) return element
def odf_create_link(url, name=None, title=None, target_frame=None, style=None, visited_style=None): """Return a text:a odf_element. Arguments: url -- str name -- unicode title -- unicode target_frame -- '_self', '_blank', '_parent', '_top' style -- string visited_style -- string """ link = odf_create_element('text:a') link.set_url(url) if name is not None: link.set_name(name) if title is not None: link.set_title(title) if target_frame is not None: link.set_target_frame(target_frame) if target_frame == '_blank': link.set_show('new') else: link.set_show('replace') if style is not None: link.set_style(style) if visited_style is not None: link.set_visited_style(visited_style) return link
def odf_create_anim_transitionFilter(smil_dur=None, smil_type=None, smil_subtype=None, smil_direction=None, smil_fadeColor=None, smil_mode=None): """ Used to make a beautiful transition between two frames. Arguments: smil_dur -- XXX complete me smil_type and smil_subtype -- see http://www.w3.org/TR/SMIL20/smil-transitions.html#TransitionEffects-Appendix to get a list of all types/subtypes smil_direction -- forward, reverse smil_fadeColor -- forward, reverse smil_mode -- in, out """ element = odf_create_element('<anim:transitionFilter/>') if smil_dur: element.set_attribute('smil:dur', smil_dur) if smil_type: element.set_attribute('smil:type', smil_type) if smil_subtype: element.set_attribute('smil:subtype', smil_subtype) if smil_direction: element.set_attribute('smil:direction', smil_direction) if smil_fadeColor: element.set_attribute('smil:fadeColor', smil_fadeColor) if smil_mode: element.set_attribute('smil:mode', smil_mode) return element
def odf_create_annotation(text_or_element=None, creator=None, date=None, name=None, parent=None): """Create an annotation element credited to the given creator with the given text, optionally dated (current date by default). If name not provided and some parent is provided, the name is autogenerated. Arguments: text -- unicode or odf_element creator -- unicode date -- datetime name -- unicode parent -- odf_element Return: odf_element """ # fixme : use offset # TODO allow paragraph and text styles element = odf_create_element('office:annotation') element.set_body(text_or_element) if creator: element.set_dc_creator(creator) if date is None: date = datetime.now() element.set_dc_date(date) if not name: name = get_unique_office_name(parent) element.set_name(name) return element
def set_user_defined_metadata(self, name, value): if type(value) is bool: value_type = 'boolean' value = u'true' if value else u'false' elif isinstance(value, (int, float, Decimal)): value_type = 'float' value = unicode(value) elif type(value) is date: value_type = 'date' value = unicode(Date.encode(value)) elif type(value) is datetime: value_type = 'date' value = unicode(DateTime.encode(value)) elif type(value) is str: value_type = 'string' value = unicode(value) elif type(value) is unicode: value_type = 'string' elif type(value) is timedelta: value_type = 'time' value = unicode(Duration.encode(value)) else: raise TypeError, 'unexpected type "%s" for value' % type(value) # Already the same element ? for metadata in self.get_element_list('//meta:user-defined'): if metadata.get_attribute('meta:name') == name: break else: metadata = odf_create_element('meta:user-defined') metadata.set_attribute('meta:name', name) self.get_meta_body().append(metadata) metadata.set_attribute('meta:value-type', value_type) metadata.set_text(value)
def odf_create_default_percentage_style(): return odf_create_element("""<number:percentage-style style:name="lpod-default-percentage-style"> <number:number number:decimal-places="2" number:min-integer-digits="1"/> <number:text>%</number:text> </number:percentage-style>""")
def __set_header_or_footer(self, text_or_element, name='header', style=u"Header"): if name == 'header': header_or_footer = self.get_header() else: header_or_footer = self.get_footer() if header_or_footer is None: header_or_footer = odf_create_element('style:' + name) self.append(header_or_footer) else: header_or_footer.clear() if not isiterable(text_or_element): # Already a header or footer? if (isinstance(text_or_element, odf_element) and text_or_element.get_tag() == 'style:%s' % name): self.delete(header_or_footer) self.append(text_or_element) return text_or_element = [text_or_element] # FIXME cyclic import from paragraph import odf_create_paragraph for item in text_or_element: if type(item) is unicode: paragraph = odf_create_paragraph(item, style=style) header_or_footer.append(paragraph) elif isinstance(item, odf_element): header_or_footer.append(item)
def odf_create_creation_time_variable(fixed=False, data_style=None): element = odf_create_element('text:creation-time') if fixed: element.set_attribute('text:fixed', 'true') if data_style is not None: element.set_style_attribute('style:data-style-name', data_style) return element
def odf_create_heading(level, text=None, restart_numbering=False, start_value=None, suppress_numbering=False, style=None): """Create a heading element of the given style and level, containing the optional given text. Level count begins at 1. Arguments: level -- int text -- unicode restart_numbering -- bool start_value -- int style -- unicode Return: odf_element """ data = '<text:h text:outline-level="%d"/>' element = odf_create_element(data % level) if text: element.set_text(text) if restart_numbering: element.set_attribute('text:restart-numbering', 'true') if start_value: element.set_attribute('text:start-value', '%s' % start_value) if suppress_numbering: element.set_attribute('text:suppress-numbering', 'true') if style: element.set_style(style) return element
def odf_create_creation_time_variable(fixed=False, data_style=None): element = odf_create_element('text:creation-time') if fixed: element.set_attribute('text:fixed', 'true') if data_style is not None: element.set_attribute('style:data-style-name', data_style) return element
def odf_create_default_percentage_style(): return odf_create_element( """<number:percentage-style style:name="lpod-default-percentage-style"> <number:number number:decimal-places="2" number:min-integer-digits="1"/> <number:text>%</number:text> </number:percentage-style>""")
def odf_create_filename_variable(display='full', fixed=False): """display can be: 'full', 'path', 'name' or 'name-and-extension' """ data = '<text:file-name text:display="%s"/>' element = odf_create_element(data % display) if fixed: element.set_attribute('text:fixed', 'true') return element
def odf_create_user_field_get(name, value, value_type=None, text=None, style=None): element = odf_create_element("text:user-field-get") element.set_attribute("text:name", name) text = _set_value_and_type(element, value=value, value_type=value_type, text=text) element.set_text(text) if style is not None: element.set_style_attribute("style:data-style-name", style) return element
def odf_create_bookmark_end(name): """ Arguments: name -- unicode """ element = odf_create_element('text:bookmark-end') element.set_attribute('text:name', name) return element
def odf_create_reference_mark_start(name): """ Arguments: name -- unicode """ element = odf_create_element('text:reference-mark-start') element.set_attribute('text:name', name) return element
def odf_create_default_time_style(): return odf_create_element( """<number:time-style style:name="lpod-default-time-style"> <number:hours number:style="long"/> <number:text>:</number:text> <number:minutes number:style="long"/> <number:text>:</number:text> <number:seconds number:style="long"/> </number:time-style>""")
def odf_create_default_date_style(): return odf_create_element( """<number:date-style style:name="lpod-default-date-style"> <number:year number:style="long"/> <number:text>-</number:text> <number:month number:style="long"/> <number:text>-</number:text> <number:day number:style="long"/> </number:date-style>""")
def set_text_content(self, text_or_element): text_box = self.get_element('draw:text-box') if text_box is None: text_box = odf_create_element('draw:text-box') self.append(text_box) if isinstance(text_or_element, odf_element): text_box.clear() return text_box.append(text_or_element) return text_box.set_text_content(text_or_element)
def odf_create_chapter_variable(display='name', outline_level=None): """display can be: 'number', 'name', 'number-and-name', 'plain-number' or 'plain-number-and-name' """ data = '<text:chapter text:display="%s"/>' element = odf_create_element(data % display) if outline_level is not None: element.set_attribute('text:outline-level', str(outline_level)) return element
def odf_create_user_field_get(name, value, value_type=None, text=None, style=None): element = odf_create_element('text:user-field-get') element.set_attribute('text:name', name) text = _set_value_and_type(element, value=value, value_type=value_type, text=text) element.set_text(text) if style is not None: element.set_style_attribute('style:data-style-name', style) return element
def odf_create_reference_mark(name): """A point reference. A point reference marks a position in text and is represented by a single <text:reference-mark> element. Consider using the wrapper: lpod.paragraph.set_reference_mark() Arguments: name -- unicode """ element = odf_create_element('text:reference-mark') element.set_name(name) return element
def odf_create_reference_mark_start(name): """The <text:reference-mark-start> element represent the start of a range reference. Consider using the wrapper: lpod.paragraph.set_reference_mark() Arguments: name -- unicode """ element = odf_create_element('text:reference-mark-start') element.set_name(name) return element
def odf_create_undividable_space(number=1): """ Arguments: number -- int Return odf_element """ # FIXME what is number? element = odf_create_element('text:s') element.set_attribute('text:c', str(number)) return element
def set_subject(self, subject): """Set the subject of the document. Arguments: subject -- unicode """ element = self.get_element('//dc:subject') if element is None: element = odf_create_element('dc:subject') self.get_meta_body().append(element) element.set_text(subject)
def set_description(self, description): """Set the description of the document. Also known as comments. Arguments: description -- unicode """ element = self.get_element('//dc:description') if element is None: element = odf_create_element('dc:description') self.get_meta_body().append(element) element.set_text(description)
def odf_create_default_currency_style(): return odf_create_element( """<number:currency-style style:name="lpod-default-currency-style"> <number:text>-</number:text> <number:number number:decimal-places="2" number:min-integer-digits="1" number:grouping="true"/> <number:text> </number:text> <number:currency-symbol number:language="fr" number:country="FR">€</number:currency-symbol> </number:currency-style>""")