Noob: Working script that replaces text from a command line and preserves fonts?

Previous topic - Next topic

noobjuk

Want to experiment creating custom pdf books with code.  Format stays the same only text changes.

Created a basic template scribus file with two text boxes called Text1 and Text2.

Modified some python code (I'm coming from .Net and new to Python) to update the text (see code below).  The setText function is breaking the fonts.  I tried to experiment with a python function but this is erroring.

My problems are:

1) 'import scribus' - can't resolve in VSCode but when I execute the script seems to work ok with this line: 
2) text fonts change when using setText
3) tried to recycle a function but this doesn't work

Does anyone have a working script that replaces text from a command line and preserves fonts etc?





# Produces a PDF after updating two default textboxes
#
# usage:
# /Applications/Scribus.app/Contents/MacOS/Scribus -g -py scribusUpdateTextThenSave.py

import scribus  # doesnt resolve but doesn't throw error during runtime
from datetime import datetime

# function doesn't work
def replaceText(text, item):
    txtwidth = scribus.getTextLength(item[0])
    scribus.insertText(text, txtwidth, item[0])
    scribus.selectText(0,txtwidth, item[0])
    scribus.deleteText(item[0])


current_dateTime = datetime.now()
firsttext = str(current_dateTime.microsecond) # generate random text

scribus.openDoc('mydoc.sla')
scribus.setText(firsttext, 'Text1')
scribus.setText('some text', 'Text2')
# replaceText(firsttext, 'Text1') # this doesn't work
pdf = scribus.PDFfile()
pdf.file = 'output1.pdf'
pdf.save()