def build_element( s ): """Add namespace to string and use TemplateXML_File to make Element""" s = s.replace(' ',' %s '%XMLNS_STR, 1) # First space ONLY return TemplateXML_File( s ).root
# Create a reverse lookup as well # e.g. REV_ODF_NAMESPACES["urn:oasis:names:tc:opendocument:xmlns:drawing:1.0"] == "draw" REV_ODF_NAMESPACES = {} for key, val in list(ODF_NAMESPACES.items()): REV_ODF_NAMESPACES[val] = key XMLNS_STR = ' '.join( ['xmlns:%s="%s"' % (sh, tag) for sh, tag in list(ODF_NAMESPACES.items())]) if __name__ == "__main__": from odpslides.template_xml_file import TemplateXML_File import sys #sys.exit() TFile = TemplateXML_File(r'D:\temp\open_office\content.xml') #TFile = TemplateXML_File(r'D:\temp\open_office_v2\GN2_Press\content.xml') for key, val in list(TFile.rev_nsOD.items()): if key not in ODF_NAMESPACES: print('%s not in ODF_NAMESPACES' % key) root = TFile.root short_pathD = TFile.short_pathD depthD = TFile.depthD print('root = %s at depth = %i' % (short_pathD[root], depthD[root])) for n in range(1, TFile.max_depth): print() for parent in root.iter(): if depthD[parent] == n: short_path = short_pathD[parent] sL = short_path.split('/')
def add_text_box( presObj, text_or_list='Test Message', text_font_colors='black #333333', x=8.0, y=2.0): """ text_font_colors can be a list of colors or a single string with space-seperated colors (e.g. "black #666666" or ["black", "#666666"]) """ if not presObj.new_content_pageL: print('...WARNING... No Last Page for TextBox:', text_or_list) return last_page = presObj.new_content_pageL[-1] # Page object # Build a color list (colorL) # If not already in list form, make a list if type(text_font_colors) != type(['r','b']): # Create a list of colors colorL = text_font_colors.split(' ') # <- make a list else: colorL = text_font_colors # <- already a list # make the colors valid colorL = [ getValidHexStr(c,'#000000') for c in colorL ] # if there's a problem with the list, make a default list if not colorL: colorL = ['#000000'] # get new style names (e.g. "a123") a_frame = presObj.get_next_a_style() # Make new Element objects and style Element objects s = TBOX_DRAW_FRAME.replace('svg:x="5.75in" svg:y="3in"','svg:x="%sin" svg:y="%sin"'%(x,y)) draw_frame = TemplateXML_File( s.replace('"a343"', '"%s"'%a_frame) ).root text_box = draw_frame.find( force_to_tag('draw:text-box') ) draw_frame_style = TemplateXML_File( TBOX_DRAW_FRAME_STYLE.replace('"a343"', '"%s"'%a_frame) ).root # get all the lines in the TextBox textL = build_text_list(text_or_list) annn_styleElemL = [(a_frame, draw_frame_style)] for itext, text in enumerate(textL): hex_col_str = colorL[ itext % len(colorL) ] a_p = presObj.get_next_a_style() a_span = presObj.get_next_a_style() p_span_elem = TemplateXML_File( TBOX_TEXT_P_SPAN.replace('"a337"', '"%s"'%a_p).replace('"a336"', '"%s"'%a_span) ).root span_elem = p_span_elem.getchildren()[0] span_elem.text = text s = TBOX_TEXT_P_STYLE.replace( 'fo:color="#000000"', 'fo:color="%s"'%hex_col_str ) p_style = TemplateXML_File( s.replace('"a337"', '"%s"'%a_p) ).root s = TBOX_TEXT_SPAN_STYLE.replace( 'fo:color="#000000"', 'fo:color="%s"'%hex_col_str ) span_style = TemplateXML_File( s.replace('"a336"', '"%s"'%a_span) ).root annn_styleElemL.append( (a_p, p_style) ) annn_styleElemL.append( (a_span, span_style) ) text_box.append( p_span_elem ) # put the new style descriptions and elements into the Presentation object. for a_new, style_elem in annn_styleElemL: presObj.new_content_styleL.append( style_elem ) presObj.new_content_styleD[ a_new ] = style_elem last_page.draw_page.append( draw_frame )
def get_empty_styles_elem(): return TemplateXML_File(styles_str)
def get_empty_content_elem(): return TemplateXML_File(content_str)
def get_manifest_elem(): return TemplateXML_File(manifest_str)