Scrambling Text: Difference between revisions

From Scribus Wiki
Jump to navigation Jump to search
Line 85: Line 85:


</syntaxhighlight>
</syntaxhighlight>
==replacetext_v2.py==

Revision as of 00:35, 8 June 2014

This article is part of the Scripts series.

I had a request to write a script that would take a text document with multiple frames and scramble its text. There was a desire to share the document regarding its layout, but not the actual content. While the text could have been replaced by lorem ipsum, this would have been time-consuming, and various styles would have to be recreated.

Here is the initial version of this script, which scrambles the text in a selected frame, which must of course be a text frame. The logic begins with the way that Autoquote takes apart the content of a frame, character by character, but skipping over the characters you're not looking for, leaving everything else, including the markers for character styles, intact.

Here we wish to change all aphabetical characters, but leave numbers, punctuation, carriage returns, and so on unchanged. It uses the python module random to generate a new random small letter and capital letter each time a new alphabetical character is found, then modifies according to the case of the found letter. This isn't like a cryptographic code using some algorithm for the process, it's as random as the random module can generate, so this will not be reversible. This isn't exactly true – use Undo after running the script to see what I mean. Once to save the document and reopen, it's truly irreversible.

replacetext.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# File: replacetext.py
# © 2014.06.06 Gregory Pittman
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
"""
USAGE

You must have a document open.


"""
import scribus
import random

if scribus.haveDoc():
    c = 0
        
else:
    scribus.messageBox('Usage Error', 'You need a Document open', icon=0, button1=1)
    sys.exit(2)

if scribus.selectionCount() == 0:
    scribus.messageBox('Scribus - Usage Error',
        "There is no object selected.\nPlease select a text frame and try again.",
        scribus.ICON_WARNING, scribus.BUTTON_OK)
    sys.exit(2)
if scribus.selectionCount() > 1:
    scribus.messageBox('Scribus - Usage Error',
        "You have more than one object selected.\nPlease select one text frame and try again.", scribus.ICON_WARNING, scribus.BUTTON_OK)
    sys.exit(2)

textbox = scribus.getSelectedObject()
pageitems = scribus.getPageItems()
boxcount = 1
for item in pageitems:
    if (item[0] == textbox):
        if (item[1] != 4):
            scribus.messageBox('Scribus - Usage Error', "This is not a textframe. Try again.", scribus.ICON_WARNING, scribus.BUTTON_OK)
            sys.exit(2)
contents = scribus.getTextLength(textbox)


while 1:
    if ((c == contents) or (c > contents)): break
    if ((c + 1) > contents - 1):
        nextchar = ' '
    else:
        scribus.selectText(c+1, 1, textbox)
        nextchar = scribus.getText(textbox)
    scribus.selectText(c, 1, textbox)
    char = scribus.getText(textbox)
    if (len(char) != 1):
        c += 1
        continue
    alpha = random.randint(1,26)
    letter = chr(alpha + 96)
    LETTER = chr(alpha + 64)
    if ((ord(char)>96)and(ord(char)<123)):
	scribus.deleteText(textbox)
	scribus.insertText(letter, c, textbox)
    if ((ord(char)>64)and(ord(char)<91)):
	scribus.deleteText(textbox)
	scribus.insertText(LETTER, c, textbox)

            
    c += 1
    contents = scribus.getTextLength(textbox)

scribus.setRedraw(1)
scribus.docChanged(1)
scribus.messageBox("Finished", "That should do it!",icon=0,button1=1)

replacetext_v2.py