def merge_styles(document, from_file, target=None, pretty=True): source = odf_get_document(from_file) document.delete_styles() document.merge_styles_from(source) type = document.get_type() # Enhance Presentation merge if type in ('presentation', 'presentation-template'): printinfo("merging presentation styles...") merge_presentation_styles(document, source) document.save(target=target, pretty=pretty) printinfo("Done (0 error, 0 warning).")
def merge_styles(document, from_file, target=None, pretty=True): source = odf_get_document(from_file) document.delete_styles() document.merge_styles_from(source) type = document.get_type() # Enhance Presentation merge if type in ("presentation", "presentation-template"): printinfo("merging presentation styles...") merge_presentation_styles(document, source) document.save(target=target, pretty=pretty) printinfo("Done (0 error, 0 warning).")
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 merge_presentation_styles(document, source): # Apply master page found source_body = source.get_body() first_page = source_body.get_draw_page() master_page_name = first_page.get_master_page() first_master_page = document.get_style('master-page', master_page_name) printinfo("master page used:", first_master_page.get_display_name()) body = document.get_body() for page in body.get_draw_pages(): page.set_style(first_page.get_style()) page.set_master_page(first_page.get_master_page()) page.set_presentation_page_layout( first_page.get_presentation_page_layout()) # Adjust layout -- will obviously work only if content is separated from # style: use of master pages, layout, etc. for presentation_class in ODF_CLASSES: first_frame = source_body.get_frame( presentation_class=presentation_class) if first_frame is None: continue # Mimic frame style position = first_frame.get_position() size = first_frame.get_size() style = first_frame.get_style() presentation_style = first_frame.get_presentation_style() for page in body.get_draw_pages(): for frame in page.get_frames( presentation_class=presentation_class): frame.set_position(position) frame.set_size(size) frame.set_style(style) frame.set_presentation_style(presentation_style) # Mimic list style (XXX only first level) if presentation_class == 'outline': list_style = find_presentation_list_style(source_body) for page in body.get_draw_pages(): for frame in page.get_frames( presentation_class='outline'): for list in frame.get_lists(): list.set_style(list_style)
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_paragraph_list(content=pattern) + body.get_heading_list(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).")
continue # A good file => Only text, spreadsheet and CSV mimetype = get_mimetype(filename) if mimetype not in (ODF_TEXT, ODF_SPREADSHEET, ODF_PRESENTATION, CSV_SHORT, CSV_LONG): printerr('Skip "%s" with unknown mimetype "%s"' % (filename, mimetype)) continue # Not yet an output_doc ? if output_doc is None: # Use the first doc as the output_doc output_doc = init_doc(filename, mimetype) output_type = output_doc.get_type() printinfo('%s document detected' % output_type.title()) elif mimetype == ODF_TEXT: # Add a text doc if output_type != 'text': print_incompatible(filename, output_type) continue add_odt(filename, output_doc) elif mimetype in (ODF_SPREADSHEET, CSV_SHORT, CSV_LONG): # Add a spreadsheet doc if output_type != 'spreadsheet': print_incompatible(filename, output_type) continue # CSV? if mimetype in (CSV_SHORT, CSV_LONG): add_csv(filename, output_doc) else:
exit(1) check_target_file(target) output_document = odf_new_document('presentation') output_meta = output_document.get_part(ODF_META) output_meta.set_title(u"Interop Budapest Demo") # Styles styles = output_document.get_part(ODF_STYLES) first_master_page = styles.get_master_page() if first_master_page is None: raise ValueError, "no master page found" for i, filename in enumerate(filenames): # TODO folders and collections printinfo("Processing %s..." % filename) result = urlsplit(filename) scheme = result.scheme if not scheme: file = open(filename) elif result.username: if result.port: netloc = '%s:%s' % (result.hostname, result.port) else: netloc = result.hostname url = urlunsplit( (scheme, netloc, result.path, result.query, result.fragment)) password_mgr = HTTPPasswordMgrWithDefaultRealm() password_mgr.add_password(None, url, result.username, result.password) handler = HTTPBasicAuthHandler(password_mgr)
def delete_styles(document, target, pretty=True): n = document.delete_styles() document.save(target=target, pretty=pretty) printinfo(str(n), "styles removed (0 error, 0 warning).")
def merge_styles(document, from_file, target=None, pretty=True): source = odf_get_document(from_file) document.delete_styles() document.merge_styles_from(source) document.save(target=target, pretty=pretty) printinfo("Done (0 error, 0 warning).")
exit(1) check_target_file(target) output_document = odf_new_document('presentation') output_meta = output_document.get_part(ODF_META) output_meta.set_title(u"Interop Budapest Demo") # Styles styles = output_document.get_part(ODF_STYLES) first_master_page = styles.get_master_page() if first_master_page is None: raise ValueError, "no master page found" for i, filename in enumerate(filenames): # TODO folders and collections printinfo("Processing %s..." % filename) result = urlsplit(filename) scheme = result.scheme if not scheme: file = open(filename) elif result.username: if result.port: netloc = '%s:%s' % (result.hostname, result.port) else: netloc = result.hostname url = urlunsplit((scheme, netloc, result.path, result.query, result.fragment)) password_mgr = HTTPPasswordMgrWithDefaultRealm() password_mgr.add_password(None, url, result.username, result.password) handler = HTTPBasicAuthHandler(password_mgr)