def set_drawframe_font_color( self, frame_class='title', font_color='black', nskip=0 ): """ Set the fo:color in all the style elements of the frame """ hex_col_str = getValidHexStr( font_color, "#000000") # default to black n_count = 0 if frame_class in self.draw_frameD: for draw_frame in self.draw_frameD[frame_class]: n_count += 1 if nskip >= n_count: continue for subelem in draw_frame.iter(): if subelem.tag == TEXT_SPAN_TAG: aNNN = subelem.get( TEXT_STYLE_NAME_ATTR, '' ) if aNNN not in self.presObj.new_content_styleD: print( 'Bad style index = %s, in set_drawframe_font_color'%aNNN ) else: span_elem = self.presObj.new_content_styleD[ aNNN ] for sub_span_elem in span_elem.iter(): if sub_span_elem.get(FONT_COLOR_ATTR, ''): sub_span_elem.set( FONT_COLOR_ATTR, hex_col_str )
def add_title_chart( self, title='My Title', subtitle='My Subtitle', title_font_color='', subtitle_font_color='',background_color="", pcent_stretch_center=0, pcent_stretch_content=0, pcent_move_content_right=None, pcent_move_content_up=None): """ Create a title slide with subtitle. :keyword str title: (default=='My Title') :keyword str subtitle: (default=='My Subtitle') :keyword str title_font_color: (default=='') :keyword str subtitle_font_color: (default=='') :keyword str background_color: (default=="") :return: None :rtype: None """ if background_color: background_color = getValidHexStr( background_color, "#ffffff" ) # default to white else: background_color = self.background_color inpD = {'title':title, 'subtitle':subtitle, 'background_color':background_color, 'title_font_color':title_font_color, 'subtitle_font_color':subtitle_font_color, 'pcent_stretch_center':pcent_stretch_center, 'pcent_stretch_content':pcent_stretch_content, 'pcent_move_content_right':pcent_move_content_right, 'pcent_move_content_up':pcent_move_content_up} new_page = Page( self, disp_name="Title Slide", **inpD) self.add_a_new_page( new_page )
def set_background(self): if self.presObj.page_type == 'solidbg': hex_col_str = getValidHexStr(self.presObj.background_color, "#ffffff") # default to white self.content_tmplt.set_all_attr_of_tag( 'style:drawing-page-properties', 'draw:fill-color', hex_col_str)
def set_footer_font_color(self): hex_col_str = getValidHexStr(self.presObj.footer_font_color, "#000000") # default to black #print('Setting page_number_font_color to',hex_col_str) self.styles_tmplt.set_all_styles_of_tag_w_attr('draw:frame', 'presentation:class', "footer", 'fo:color', hex_col_str)
def set_background(self): if self.presObj.page_type == 'solidbg': hex_col_str = getValidHexStr( self.presObj.background_color , "#ffffff") # default to white self.styles_tmplt.set_all_attr_of_tag( 'style:drawing-page-properties', 'draw:fill-color', hex_col_str) elif self.presObj.page_type == 'grad': hex_col_str = getValidHexStr( self.presObj.grad_start_color , "#ffffff") # default to white self.styles_tmplt.set_all_attr_of_tag( 'draw:gradient', 'draw:start-color', hex_col_str) #print('Setting start gradient color to', hex_col_str) hex_col_str = getValidHexStr( self.presObj.grad_end_color , "#ffffff") # default to white self.styles_tmplt.set_all_attr_of_tag( 'draw:gradient', 'draw:end-color', hex_col_str) #print('Setting end gradient color to', hex_col_str) self.styles_tmplt.set_all_attr_of_tag( 'draw:gradient', 'draw:style', self.presObj.grad_draw_style) self.styles_tmplt.set_all_attr_of_tag( 'draw:gradient', 'draw:angle', self.presObj.grad_angle) elif self.presObj.page_type == 'image': img_name = 'media/%s'%self.presObj.image_nameL[0] self.styles_tmplt.set_all_attr_of_tag( 'draw:fill-image', 'xlink:href', img_name)
def set_background(self): if self.presObj.page_type == 'solidbg': hex_col_str = getValidHexStr(self.presObj.background_color, "#ffffff") # default to white self.styles_tmplt.set_all_attr_of_tag( 'style:drawing-page-properties', 'draw:fill-color', hex_col_str) elif self.presObj.page_type == 'grad': hex_col_str = getValidHexStr(self.presObj.grad_start_color, "#ffffff") # default to white self.styles_tmplt.set_all_attr_of_tag('draw:gradient', 'draw:start-color', hex_col_str) #print('Setting start gradient color to', hex_col_str) hex_col_str = getValidHexStr(self.presObj.grad_end_color, "#ffffff") # default to white self.styles_tmplt.set_all_attr_of_tag('draw:gradient', 'draw:end-color', hex_col_str) #print('Setting end gradient color to', hex_col_str) self.styles_tmplt.set_all_attr_of_tag('draw:gradient', 'draw:style', self.presObj.grad_draw_style) self.styles_tmplt.set_all_attr_of_tag('draw:gradient', 'draw:angle', self.presObj.grad_angle) elif self.presObj.page_type == 'image': img_name = 'media/%s' % self.presObj.image_nameL[0] self.styles_tmplt.set_all_attr_of_tag('draw:fill-image', 'xlink:href', img_name)
def add_title_chart(self, title='My Title', subtitle='My Subtitle', title_font_color='', subtitle_font_color='', background_color="", pcent_stretch_center=0, pcent_stretch_content=0, pcent_move_content_right=None, pcent_move_content_up=None): """ Create a title slide with subtitle. :keyword str title: (default=='My Title') :keyword str subtitle: (default=='My Subtitle') :keyword str title_font_color: (default=='') :keyword str subtitle_font_color: (default=='') :keyword str background_color: (default=="") :return: None :rtype: None """ if background_color: background_color = getValidHexStr(background_color, "#ffffff") # default to white else: background_color = self.background_color inpD = { 'title': title, 'subtitle': subtitle, 'background_color': background_color, 'title_font_color': title_font_color, 'subtitle_font_color': subtitle_font_color, 'pcent_stretch_center': pcent_stretch_center, 'pcent_stretch_content': pcent_stretch_content, 'pcent_move_content_right': pcent_move_content_right, 'pcent_move_content_up': pcent_move_content_up } new_page = Page(self, disp_name="Title Slide", **inpD) self.add_a_new_page(new_page)
def set_drawframe_font_color( presObj, draw_page, frame_class='title', font_color='black' ): """ Set the fo:color in all the style elements of the frame """ hex_col_str = getValidHexStr( font_color, "#000000") # default to black for elem in draw_page.iter(): if elem.tag == DRAW_FRAME_TAG: if elem.get( PRESENTATION_CLASS_ATTR, '' ) == frame_class: # Now we are in proper "title", "subtitle", etc. for subelem in elem.iter(): if subelem.tag == TEXT_SPAN_TAG: aNNN = subelem.get( TEXT_STYLE_NAME_ATTR, '' ) a_int = int( aNNN[1:] ) if a_int >= len(presObj.new_content_styleL): print( 'Bad style index = %i, when len new_content_styleL = %i'%\ (a_int, len(presObj.new_content_styleL)) ) else: span_elem = presObj.new_content_styleL[ a_int ] for sub_span_elem in span_elem.iter(): if sub_span_elem.get(FONT_COLOR_ATTR, ''): sub_span_elem.set( FONT_COLOR_ATTR, hex_col_str )
def __init__(self, title_font_color='black', subtitle_font_color='#666666', background_image="", background_color="white", grad_start_color="", grad_end_color="", grad_angle_deg=0, grad_draw_style='linear', show_date=False, date_font_color='#666666', footer="", footer_font_color='#666666', show_page_numbers=True, page_number_font_color="#666666"): """ Main Presentation object """ self.filename = None # keep a list of images for inclusion in ODP file self.image_nameD = { } # index=file system name, value=internal image name self.sys_image_nameD = { } # index=internal image name, value=file system name self.image_nameL = [] # ordered list of internal image names self.image_sizeD = { } # index=internal image name, value=tuple of image size (w,h) self.max_image_int = 0 self.title_font_color = title_font_color self.subtitle_font_color = subtitle_font_color self.background_image = background_image if self.background_image: self.internal_background_image = self.get_next_image_name( background_image) else: self.internal_background_image = '' self.background_color = getValidHexStr(background_color, "#ffffff") # default to white self.grad_start_color = grad_start_color self.grad_end_color = grad_end_color self.grad_angle = '%s' % (int(grad_angle_deg) * 10, ) # odp understands tenths of deg input self.grad_draw_style = grad_draw_style if self.internal_background_image: self.ref_odp_filename = 'ppt_all_layouts_image.odp' self.page_type = 'image' # "solidbg", "grad", "image" elif self.grad_start_color and self.grad_end_color: self.ref_odp_filename = 'ppt_all_layouts_grad.odp' self.page_type = 'grad' # "solidbg", "grad", "image" else: self.ref_odp_filename = 'ppt_all_layouts_solidbg.odp' self.page_type = 'solidbg' # "solidbg", "grad", "image" # open reference odp file self.full_odp_ref_path = os.path.join(here, 'templates', self.ref_odp_filename) self.odp_ref = ODPFile(self.full_odp_ref_path) self.styles_xml_obj = StylesXML(self, self.odp_ref) self.content_xml_obj = ContentXML(self, self.odp_ref) # figure out max style:name (e.g. "a123") self.odp_ref.styles_xml_obj.init_all_annn_style8name() self.show_date = show_date self.date_font_color = date_font_color self.footer = footer self.footer_font_color = footer_font_color self.show_page_numbers = show_page_numbers self.page_number_font_color = page_number_font_color # style names will be in order, "a0", "a1", "a2", ... self.new_content_styleL = [ ] # style:style elements to be added to auto_styles self.new_content_pageL = [ ] # draw:page elements to be added to presentation self.new_master_styleL = [ ] # For each new_content_pageL item, there are many master-page style elements self.new_master_page_styleL = [ ] # For each new_content_pageL item, there is a style:master-page item self.new_styles_office_stylesL = [ ] # usually draw:gradient or draw:fill-image statements self.new_content_styleD = {} # index="a123", value=style elem #self.new_master_styleD = {} # index="a123", value=style elem # style names and id values start at 0 (i.e. "a0", "a1", ... and "id0", "id1", ...) #self.max_style_name_int = -1 # used for nameing styles (ex. draw:style-name="a123") #self.max_draw_id_int = -1 # some internal use ??? self.max_style_name_int = self.odp_ref.styles_xml_obj.max_annn_def + 1000 self.max_draw_id_int = self.odp_ref.styles_xml_obj.max_idnnn_def + 1000
def set_background(self): if self.presObj.page_type == 'solidbg': hex_col_str = getValidHexStr( self.presObj.background_color , "#ffffff") # default to white self.content_tmplt.set_all_attr_of_tag( 'style:drawing-page-properties', 'draw:fill-color', hex_col_str)
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 set_footer_font_color(self): hex_col_str = getValidHexStr( self.presObj.footer_font_color , "#000000") # default to black #print('Setting page_number_font_color to',hex_col_str) self.styles_tmplt.set_all_styles_of_tag_w_attr( 'draw:frame', 'presentation:class', "footer", 'fo:color', hex_col_str)
def __init__(self, title_font_color='black', subtitle_font_color='#666666', background_image="", background_color="white", grad_start_color="", grad_end_color="", grad_angle_deg=0, grad_draw_style='linear', show_date=False, date_font_color='#666666', footer="", footer_font_color='#666666', show_page_numbers=True, page_number_font_color="#666666"): """ Main Presentation object """ self.filename = None # keep a list of images for inclusion in ODP file self.image_nameD = {} # index=file system name, value=internal image name self.sys_image_nameD = {} # index=internal image name, value=file system name self.image_nameL = [] # ordered list of internal image names self.image_sizeD = {} # index=internal image name, value=tuple of image size (w,h) self.max_image_int = 0 self.title_font_color = title_font_color self.subtitle_font_color = subtitle_font_color self.background_image = background_image if self.background_image: self.internal_background_image = self.get_next_image_name( background_image ) else: self.internal_background_image = '' self.background_color = getValidHexStr( background_color, "#ffffff" ) # default to white self.grad_start_color = grad_start_color self.grad_end_color = grad_end_color self.grad_angle = '%s'%(int(grad_angle_deg)*10, ) # odp understands tenths of deg input self.grad_draw_style = grad_draw_style if self.internal_background_image: self.ref_odp_filename = 'ppt_all_layouts_image.odp' self.page_type = 'image' # "solidbg", "grad", "image" elif self.grad_start_color and self.grad_end_color: self.ref_odp_filename = 'ppt_all_layouts_grad.odp' self.page_type = 'grad' # "solidbg", "grad", "image" else: self.ref_odp_filename = 'ppt_all_layouts_solidbg.odp' self.page_type = 'solidbg' # "solidbg", "grad", "image" # open reference odp file self.full_odp_ref_path = os.path.join( here, 'templates', self.ref_odp_filename ) self.odp_ref = ODPFile( self.full_odp_ref_path ) self.styles_xml_obj = StylesXML( self, self.odp_ref ) self.content_xml_obj = ContentXML(self, self.odp_ref ) # figure out max style:name (e.g. "a123") self.odp_ref.styles_xml_obj.init_all_annn_style8name() self.show_date = show_date self.date_font_color = date_font_color self.footer = footer self.footer_font_color = footer_font_color self.show_page_numbers = show_page_numbers self.page_number_font_color = page_number_font_color # style names will be in order, "a0", "a1", "a2", ... self.new_content_styleL = [] # style:style elements to be added to auto_styles self.new_content_pageL = [] # draw:page elements to be added to presentation self.new_master_styleL = [] # For each new_content_pageL item, there are many master-page style elements self.new_master_page_styleL = [] # For each new_content_pageL item, there is a style:master-page item self.new_styles_office_stylesL = [] # usually draw:gradient or draw:fill-image statements self.new_content_styleD = {} # index="a123", value=style elem #self.new_master_styleD = {} # index="a123", value=style elem # style names and id values start at 0 (i.e. "a0", "a1", ... and "id0", "id1", ...) #self.max_style_name_int = -1 # used for nameing styles (ex. draw:style-name="a123") #self.max_draw_id_int = -1 # some internal use ??? self.max_style_name_int = self.odp_ref.styles_xml_obj.max_annn_def + 1000 self.max_draw_id_int = self.odp_ref.styles_xml_obj.max_idnnn_def + 1000