Old script very slow on Scribus 1.5.8.

Previous topic - Next topic

Ribus

Hello,
This script is very low on my Scribus 5.8. under Win10 64 bits.
I use it for cleaning an html prepared from a Word file after a macro working on cyrillic and french.
I have to replace unvisbles Unicods characters.
I suppose I need to adapt it for Python 3.0
If anybody couls help me,
Many tahnks,
Richard
------------------------

Code (python) Select
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Author: Me  8)
File: RR_Article.py (Clean-up the imported text from Openoffice html.)
Version: 1.0
Date: 11/11/2021
"""

import sys, re
try:
    from scribus import *
except ImportError:
    print ('Ce script est écrit en Python. Il ne peut être lancé que depuis Scribus.')
    sys.exit(1)
if not scribus.haveDoc():
    scribus.messageBox ('Warning', 'Vous devez ouvrir un document.', scribus.ICON_WARNING, scribus.BUTTON_OK)
    sys.exit(1)
if scribus.selectionCount() == 0:
    scribus.messageBox ('Warning', 'Vous devez sélectionner un cadre de texte.', scribus.ICON_WARNING, scribus.BUTTON_OK)
    sys.exit(1)
if scribus.selectionCount() > 1:
    scribus.messageBox ('Warning', 'Vous devez sélectionner un cadre de texte.', scribus.ICON_WARNING, scribus.BUTTON_OK)
    sys.exit(1)

replacements = (

    # esp ins etroit = "\u202F", Esp. Ins = "\u00A0", Esp = "\u0020", Point = "\u002E"
    # virgule = "\u002C", ¤ = "\u00A4", Esp = "\u0020", Point = "\u002E"
   
    ("\u00A4","\u0020"),                # ¤ en espace   
    ("\u0020"+"\u20AC","\u202F"+"\u20AC"),        # espace + € to espace insécable étroit + €
    ("\u0020"+"\u0070"+"\u002E"+"\u0020","\u00A0+\u0070+\u002E+\u0020"),        # epace p. espace en insécable p. espace
    ("\u0020"+"\u0070"+"\u002E"+"\u002C"+"\u0020","\u00A0"+"\u0070"+"\u002E"+"\u002C"+"\u0020"),        # p., en insécable point virgule
    (" № ","\u0020"+"\u2116"+"\u00A0"),        # № en espace N° insécable
    ("\u00AB"+"\u00BB","\u00AB"),            # parenthèse O/F en ouvrante
    ("\u2019"+"\u00BB","\u2019"+"\u00AB"),        # "parenthèse fermante en "ouvrante
    ("\u00A7","\u202F"),                # § en U+202F NARROW NO-BREAK SPACE
    ("\u0020"+"\u0020","\u0020"),            # deux espaces en un
    ("""\(""",""" ("""),                # une parenthèse ouvrantes en espace +
    ("\u00ab"+"\u0020","\u00ab"+"\u202f"),        # U+00AB LEFT-POINTING DOUBLE ANGLE QUOTATION MARK
    ("\u0020"+"\u00bb","\u202f"+"\u00bb"),        # U+00BB RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK    ("\u0020"+"\u003b","\u202f"+"\u003b"),    # U+003B SEMICOLON
    ("\u0020"+"\u202f","\u202f"),            # U+202F NARROW NO-BREAK SPACE
    ("\u202f"+"\u0020","\u202f"),            # U+202F NARROW NO-BREAK SPACE
    ("""atel'""","""atelʹ"""),            # signe mou \u02CA
    ("""d't""","""dʹt"""),                # signe mou
    ("""s'm""","""sʹm"""),                # signe mou
    ("""L'C""","""LʹC"""),                # signe mou
    ("""l'k""","""lʹk"""),                # signe mou
    ("""l'n""","""lʹn"""),                # signe mou
    ("""l't""","""lʹt"""),                # signe mou
    ("""n'g""","""nʹg"""),                # signe mou
    ("""l'k""","""lʹk"""),                # signe mou
    ("""st'""","""stʹ"""),                # signe mou
    ("""st'""","""stʹ"""),                # signe mou
    ("""zn'""","""znʹ"""),                # signe mou
    (""",""",""", """),                # virgule collée
    ("\u0020"+"\u0021","\u00A0"+"\u0021"),        # point virgule insécable
    ("\u0020"+"\u003A","\u00A0"+"\u003A"),        # espace : par insécable :
    ("\u0020"+"\u00A0","\u00A0"),            # espace insécable par insecable
    ("""  """,""" """),                # 3 espaces en un
    ("\u0020"+"\u0020","\u0020"),            # 2 espaces en un
)

d = scribus.getSelectedObject()
if scribus.getObjectType(d) != "TextFrame":
    scribus.messageBox ("Warning", "Vous devez sélectionner un cadre de texte.", scribus.ICON_WARNING, scribus.BUTTON_OK)
    sys.exit(1)
else:
    for item in replacements:
        content = (scribus.getAllText(d))
        p = re.compile (item
[o])
        r = re.finditer (p, content)
        for i in reversed (tuple(r)):
            count = i.end()-i.start()
            scribus.selectText (i.start(), count, d)
            scribus.deleteText(d)
            scribus.insertText (item[1], i.start(), d)
    scribus.setRedraw (True)
    scribus.docChanged (True)
    scribus.messageBox ("Info", "Script terminé avec succès.", scribus.ICON_INFORMATION, scribus.BUTTON_OK)

a.l.e

#1
this

        p = re.compile (item
[o])

is clearly wrong...

but i can't really figure out what "[ o ]" is supposed to be....

btw, is there any reason why you are using this script instead of the one that jluc is maintaining?
(ok, perhaps you're not just applying the french space rules... no idea what exactly your script is doing... just replacing the first item in the pair with the second one?)





a.l.e

#2
while i was rewriting your script i've noticed a few things:

  • you probably do not need to use the regular expression module
  • you calculate the positions of the replacements with "finditer" and *then* go to the resulting positions and delete + insert: when the amount of chars deleted and inserted are not the same, the index on "content" will not match  the position in the text frame anymore and "selectText" will not do what you expect. it's probably better to search, one match at a time, and start from the index of the latest match.
  • you activate the redraw towards the end of the script, but you never disable it... that might be a reason why you experience a slow down.
  • why are you using triple quotes for single line strings?
  • why are you replacing "atel'" by "atel'"... the same applies for all the other "signe mous"?
  • you probably don't need to "getAllText()" for each pattern: just replace in content and the string

Here is a revised version of your script that seems to work for me (but i can't try it for real, since i don't have the "bad chars" to replace in any document:

Code (python) Select
# -*- coding: utf-8 -*-

import sys
try:
    import scribus
except ImportError:
    print('Ce script est écrit en Python. Il ne peut être lancé que depuis Scribus.')
    sys.exit(1)

if (not scribus.haveDoc() or
        scribus.selectionCount() != 1 or
        scribus.getObjectType() != "TextFrame"):
    scribus.messageBox('Warning', 'Vous devez sélectionner un cadre de texte.')
    sys.exit(1)

replacements = (
    # esp ins etroit = "\u202F", Esp. Ins = "\u00A0", Esp = "\u0020", Point = "\u002E"
    # virgule = "\u002C", ¤ = "\u00A4", Esp = "\u0020", Point = "\u002E"
  
    ("\u00A4","\u0020"),                # ¤ en espace  
    ("\u0020"+"\u20AC","\u202F"+"\u20AC"),        # espace + € to espace insécable étroit + €
    ("\u0020"+"\u0070"+"\u002E"+"\u0020","\u00A0+\u0070+\u002E+\u0020"),        # epace p. espace en insécable p. espace
    ("\u0020"+"\u0070"+"\u002E"+"\u002C"+"\u0020","\u00A0"+"\u0070"+"\u002E"+"\u002C"+"\u0020"),        # p., en insécable point virgule
    (" № ","\u0020"+"\u2116"+"\u00A0"),        # № en espace N° insécable
    ("\u00AB"+"\u00BB","\u00AB"),            # parenthèse O/F en ouvrante
    ("\u2019"+"\u00BB","\u2019"+"\u00AB"),        # "parenthèse fermante en "ouvrante
    ("\u00A7","\u202F"),                # § en U+202F NARROW NO-BREAK SPACE
    ("\u0020"+"\u0020","\u0020"),            # deux espaces en un
    ("\("," ("),                # une parenthèse ouvrantes en espace +
    ("\u00ab"+"\u0020","\u00ab"+"\u202f"),        # U+00AB LEFT-POINTING DOUBLE ANGLE QUOTATION MARK
    ("\u0020"+"\u00bb","\u202f"+"\u00bb"),        # U+00BB RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK
    ("\u0020"+"\u003b","\u202f"+"\u003b"),    # U+003B SEMICOLON
    ("\u0020"+"\u202f","\u202f"),            # U+202F NARROW NO-BREAK SPACE
    ("\u202f"+"\u0020","\u202f"),            # U+202F NARROW NO-BREAK SPACE
    ("atel'","atelʹ"),            # signe mou \u02CA
    ("d't","dʹt"),                # signe mou
    ("s'm","sʹm"),                # signe mou
    ("L'C","LʹC"),                # signe mou
    ("l'k","lʹk"),                # signe mou
    ("l'n","lʹn"),                # signe mou
    ("l't","lʹt"),                # signe mou
    ("n'g","nʹg"),                # signe mou
    ("l'k","lʹk"),                # signe mou
    ("st'","stʹ"),                # signe mou
    ("st'","stʹ"),                # signe mou
    ("zn'","znʹ"),                # signe mou
    (",",", "),                # virgule collée
    ("\u0020"+"\u0021","\u00A0"+"\u0021"),        # point virgule insécable
    ("\u0020"+"\u003A","\u00A0"+"\u003A"),        # espace : par insécable :
    ("\u0020"+"\u00A0","\u00A0"),            # espace insécable par insecable
    ("  "," "),                # 3 espaces en un
    ("\u0020"+"\u0020","\u0020"),            # 2 espaces en un
)

content = scribus.getAllText()
for item in replacements:
    start = 0
    while start >= 0:
        start = content.find(item[0], start)
        if start == -1:
            continue
        count = len(item[0])
        scribus.selectText(start, count)
        scribus.deleteText()
        scribus.insertText(item[1], start)
        content = content[0:start] + item[1] + content[start + count:]
        start += len(item[1])

scribus.layoutText()
scribus.docChanged(True)
scribus.messageBox("Info", "Script terminé avec succès.")

i could not stop on such a good path, and i've created a tiny and more general script that can be used for doing a set of standard replacements:

https://github.com/aoloe/scribus-script-repository/tree/master/replace-patterns