#!/usr/bin/env python3 import easymacro as app ID_EXTENSION = '' _ = None def _replace_styles(): dlg = _create_dialog_replace_styles() dlg.open() return class Controllers(object): def __init__(self, dlg): self.d = dlg @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 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 def cmd_close_action(self, event): self.d.close() return def _create_dialog_replace_styles(): args = { 'Name': 'dialog', 'Title': _('Replace Styles'), 'Width': 220, 'Height': 100, } 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': 15, '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': '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) dlg.lst_paragraph_target.move(dlg.lst_paragraph_source, 5, 0) dlg.cmd_replace_by_paragraph.move(dlg.lst_paragraph_target, 5, 0) dlg.cmd_close.move(dlg.lst_paragraph_source, 0, 20) dlg.cmd_close.center() doc = app.active styles = doc.styles['Paragraph'].names dlg.lst_paragraph_source.data = styles dlg.lst_paragraph_target.data = styles return dlg @app.catch_exception def run(args, path_locales): global _ _ = app.install_locales(path_locales) if args == 'replacestyles': _replace_styles() return