Scribus Forums

Scribus => Scripts and Plugins => Topic started by: a.l.e on November 22, 2026, 11:16:22 AM

Title: Loading Python libraries, fanzines and imposition
Post by: a.l.e on November 22, 2026, 11:16:22 AM
Several people have been asking about loading Python libraries for their scripts running inside of Scribus.

While it was (probably) already possible by starting Scribus from a terminal having access to those library or by globally installing the packages, that was not a practical solution, when you wanted to distribute your script.

Some time ago, I've discovered that it's actually possible to "load" a virtual environment and access the locally installed packages from a Script.
It's somehow easy for many libraries, other (like numpy) need to load it in a new thread.

The mechanism at its core is rather simple.
It gets a bit more verbose after having added a few checks, and made it a function that can be used in multiple contexts:

def load_venv(packages):
    """load_venv(packages) -> bool

       Load the virtual environment in venv with the packages

       packages -- A dict, the key being the name of the package and the value
                   a folder in site-packages to test if the package is indeed
                   installed.
                   It can be a list if the package name is identical to
                   the folder name.
    """
    ACTIVATE_PATH = Path(__file__).resolve().parent
    venv_path = ACTIVATE_PATH / 'venv'
    if not venv_path.is_dir():
        show_error(f'You need to create a virtual environment called venv in the script folder:\n\n{venv_path}.\n\nSee the README.md for more details.')
        return False

    # Find site-packages on Windows or Linux/Mac
    site_packages = None

    for pattern in ('Lib/site-packages', 'lib/python*/site-packages'):
        matches = list(venv_path.glob(pattern))
        if matches:
            site_packages = matches[0]
            break

    if site_packages is None:
        show_error("Could not find the site-packages in the virtual environment.")
        return False

    site.addsitedir(site_packages)

    if type(packages) is list:
        packages = dict(zip(packages, packages))
    for package, folder in packages.items():
        if not (site_packages / folder).is_dir():
            show_error(f'You need to pip install {package} in your virtual environment\n\n{ACTIVATE_PATH}.\n\n See the README.md for more details.')
            return False

    return True

Where show_error is:

def show_error(message):
    scribus.messageBox('Scribus - Script Error', message, scribus.ICON_WARNING, scribus.BUTTON_OK)

The other nice discovery from a couple of weeks ago is that there is pure Python package for manipulating PDFs: pypdf!

I've used it for a simple "project management" script I've created for a specific task.
It can merge PDFs from multiple Scribus documents and place two A5 pages on one A4 landscape page or just add the pages that are already A4 landscape in the original document.
It does not work yet from inside of Scribus, but I plan to improve it a bit (automatically update the starting page number...), make it work from inside Scribus and then also publish it here.

But there is another project, where I've used the newly acquired skills.
Lately, a few people bragged about doing foldable fanzines:

https://www.quarantinepubliclibrary.com/tutorial

Shouldn't it be possible to do it also with Scribus?

Here is my new script for it:

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

This is how the file looks in Scribus:

fanzine-scribus.png

And the imposed PDF:

fanzine.png

Just run the script, with your A7 document open in Scribus and you'll get a PDF with the same name next to it. No questions asked (yes, it will overwrite any existing file, I might add a confirmation in the future...)

The most interesting part: this is a proof of concept for adding imposition to Scribus.
Personally, I'm for creating multiple specific scripts, rather than a highly complex interface for doing every possible type of imposition!

Stay tuned...