💾 Archived View for log.pfad.fr › 2023 › july-update › scribus-cookbook.py captured on 2024-08-18 at 17:54:15.
⬅️ Previous capture (2023-09-08)
-=-=-=-=-=-=-
#!/usr/bin/env python # -*- coding: utf-8 -*- # SPDX-FileCopyrightText: 2023 Olivier Charvin <git@olivier.pfad.fr> # SPDX-License-Identifier: CC0-1.0 import sys try: import scribus except ImportError: print("This Python script is written for the Scribus scripting interface.") print("It can only be run from within Scribus.") sys.exit(1) # adjust depending on doc attrNamePerStyle = { "Salty title": "TOC_salty", "Sweet title": "TOC_sweet", } def adjustedAttr(name, key, value): attr = scribus.getObjectAttributes(name) styleValues = attrNamePerStyle.values() reducedAttr = [a for a in attr if a.get("Name") not in styleValues] reducedAttr.append( { "Name": key, "Value": value, "Type": "string", "Parameter": "", "Relationship": "none", "RelationshipTo": "", "AutoAddTo": "none", } ) return reducedAttr def adjustTitleAttributes(name): style = scribus.getParagraphStyle(name) key = attrNamePerStyle.get(style, None) if not key: return title = scribus.getAllText(name).strip().partition("\r")[0].strip() attr = adjustedAttr(name, key, title) scribus.setObjectAttributes(attr, name) print(style, title) def main(argv): page = 0 pagenum = scribus.pageCount() scribus.progressTotal(pagenum) while page < pagenum: scribus.progressSet(page) page += 1 scribus.gotoPage(page) d = scribus.getPageItems() flipBookImageName = None # Check all items of the page for name, itemType, _ in d: if itemType == 4: # TextFrame (automatic TableOfContent generation) adjustTitleAttributes(name) continue if itemType == 2: # ImageFrame (flipbook) for a in scribus.getObjectAttributes(name): if a.get("Name") != "flipbook": continue if flipBookImageName: raise Exception("Duplicate flipBook on page {}".format(page)) flipBookImageName = name if page > 1 and page % 2 == 1: if not flipBookImageName: raise Exception("Missing flipBook on page {}".format(page)) path = "/path/to/synfig/animation.{:04d}.png".format( (page - 3) // 2 ) scribus.loadImage(path, flipBookImageName) scribus.setScaleImageToFrame(True, True, flipBookImageName) def main_wrapper(argv): """The main_wrapper() function disables redrawing, sets a sensible generic status bar message, and optionally sets up the progress bar. It then runs the main() function. Once everything finishes it cleans up after the main() function, making sure everything is sane before the script terminates.""" try: scribus.messagebarText("Running script...") scribus.progressReset() main(argv) finally: # Exit neatly even if the script terminated with an exception, # so we leave the progress bar and status bar blank and make sure # drawing is enabled. if scribus.haveDoc(): scribus.setRedraw(True) scribus.progressReset() scribus.messagebarText("Extras > Generate Table Of Contents") # This code detects if the script is being run as a script, or imported as a module. # It only runs main() if being run as a script. This permits you to import your script # and control it manually for debugging. if __name__ == "__main__": main_wrapper(sys.argv)