#!/usr/bin/env python3 import easymacro as app ID_EXTENSION = '' _ = None class Controllers(object): def __init__(self, dlg): self.d = dlg self.para_styles = {} self.char_styles = {} @app.catch_exception def cmd_replace_by_paragraph_action(self, event): source = self.d.lst_paragraph_source.value target = self.d.lst_paragraph_target.value if not source: msg = _('Select style source') app.errorbox(msg) return if not target: msg = _('Select style target') app.errorbox(msg) return if source == target: msg = _('Select different styles') app.errorbox(msg) return msg = _('Replace selected styles?') if not app.question(msg): return source = self.para_styles[source] target = self.para_styles[target] doc = app.active i = 0 for p in doc.paragraphs: if not p.is_paragraph: continue if p.style == source: p.style = target i += 1 self.d.close() app.debug(f'{i} replaces') return @app.catch_exception def cmd_replace_by_character_action(self, event): source = self.d.lst_character_source.value target = self.d.lst_character_target.value if not source: msg = _('Select style source') app.errorbox(msg) return if not target: msg = _('Select style target') app.errorbox(msg) return if source == target: msg = _('Select different styles') app.errorbox(msg) return msg = _('Replace selected styles?') if not app.question(msg): return source = self.char_styles[source] target = self.char_styles[target] doc = app.active i = 0 for paragraph in doc.paragraphs: if not paragraph.is_paragraph: continue for p in paragraph: if p.style == source: p.style = target i += 1 self.d.close() app.debug(f'{i} replaces') return def cmd_close_action(self, event): self.d.close() return def _create_dialog_replace_styles(): args = { 'Name': 'dialog', 'Title': _('Replace Styles'), 'Width': 220, 'Height': 125, } dlg = app.create_dialog(args) dlg.id = ID_EXTENSION dlg.events = Controllers args = { 'Type': 'Label', 'Name': 'lbl_paragraphs', 'Label': _('Replace by paragraph'), 'Width': 70, 'Height': 10, 'X': 10, 'Y': 10, } dlg.add_control(args) args = { 'Type': 'ListBox', 'Name': 'lst_paragraph_source', 'Width': 85, 'Height': 15, 'Dropdown': True, } dlg.add_control(args) args = { 'Type': 'ListBox', 'Name': 'lst_paragraph_target', 'Width': 85, 'Height': 15, 'Dropdown': True, } dlg.add_control(args) args = { 'Type': 'Button', 'Name': 'cmd_replace_by_paragraph', 'Width': 15, 'Height': 15, 'ImageURL': 'replace.svg', 'ImagePosition': 1, } dlg.add_control(args) args = { 'Type': 'Label', 'Name': 'lbl_character', 'Label': _('Replace by character'), 'Width': 70, 'Height': 10, } dlg.add_control(args) args = { 'Type': 'ListBox', 'Name': 'lst_character_source', 'Width': 85, 'Height': 15, 'Dropdown': True, } dlg.add_control(args) args = { 'Type': 'ListBox', 'Name': 'lst_character_target', 'Width': 85, 'Height': 15, 'Dropdown': True, } dlg.add_control(args) args = { 'Type': 'Button', 'Name': 'cmd_replace_by_character', 'Width': 15, 'Height': 15, 'ImageURL': 'replace.svg', 'ImagePosition': 1, } dlg.add_control(args) args = { 'Type': 'Button', 'Name': 'cmd_close', 'Label': _('~Close'), 'Width': 70, 'Height': 20, 'ImageURL': 'close.svg', 'ImagePosition': 1, } dlg.add_control(args) dlg.lst_paragraph_source.move(dlg.lbl_paragraphs, 0, 5) dlg.lst_paragraph_target.move(dlg.lst_paragraph_source, 5, 0) dlg.cmd_replace_by_paragraph.move(dlg.lst_paragraph_target, 5, 0) dlg.lbl_character.move(dlg.lst_paragraph_source, 0, 10) dlg.lst_character_source.move(dlg.lbl_character, 0, 5) dlg.lst_character_target.move(dlg.lst_character_source, 5, 0) dlg.cmd_replace_by_character.move(dlg.lst_character_target, 5, 0) dlg.cmd_close.move(dlg.lst_character_source, 0, 15, True) doc = app.active styles = doc.styles['Paragraph'].names dlg.lst_paragraph_source.data = tuple(styles.keys()) dlg.lst_paragraph_target.data = tuple(styles.keys()) dlg.events.para_styles = styles styles = doc.styles['Character'].names dlg.lst_character_source.data = tuple(styles.keys()) dlg.lst_character_target.data = tuple(styles.keys()) dlg.events.char_styles = styles return dlg def _replace_styles(): dlg = _create_dialog_replace_styles() dlg.open() return def run(args, path_locales): global _ _ = app.install_locales(path_locales) if args == 'replacestyles': _replace_styles() return