Generate publishing by code with Scribus

Simple guide to use Scribus with docker and python

Nicola Landro
3 min readApr 1, 2024

Scribus is a libre software for publishing like Adobe InDesign. In this article we will see how to fill scribus templates with python generating programmatically custom publishing.

The project can be find in that codebase.
There are also another article that create document using latex but in this case it is possible to create template using Scribus instead latex.

Create Scribus file

At first we can generate a scribus file: template.sla putting inside the box the variables as %%VARIABLE%% after we will replace it with python.

Modify sla file with python

For modify the file we pyscribus start by preparing the script/pyscibus_templating.py :

# https://etnadji.fr/pyscribus/guide/en/templating.html
import pyscribus.file as pyf

# intro.sla is parsed at instanciation
parsed = pyf.ScribusFile("1.5.5", "template.sla", templating=True)
stories = parsed.templatable_stories

datas = {
"%Title%": "My title",
"%Text%": "Lorem ipsum",
"%Caption%": "Caption",
"%Footer%": "Footer"
}

# For each stories, we replace/feed the placeholders with their contents

for index, templatable_story in enumerate(stories):
print(index, templatable_story)
parsed.stories.feed(templatable_story, datas)

# Change image
image_frames = parsed.pageobjects.filter(object_type="image")
imgbox = image_frames[0]
xml = imgbox.toxml()
xml.attrib["PFILE"] = './fake.jpeg'
imgbox.fromxml(xml)

# Saving the templated document as a new one
parsed.save("templated.sla")

Preare the docker/DockerfilePy :

FROM python:3.11

RUN pip install --upgrade pip && pip install pyscribus

WORKDIR /data

CMD python ./scripts/pyscribus_templateing.py

Prepare docker-compose.yml :

services:
python:
build:
context: .
dockerfile: docker/DockerfilePy
volumes:
- .:/data

Now run with docker compose run python it will generate generated.sla file with replacement of text and images (is important that the file fake.jpg is in the same folder because we use relative path).

Autogenerate PDF

For generating the pdf we will use the scribus command line so we start preparing a python script for scribus cli scripts/to-pdf.py

# Produces a PDF for the SLA passed as a parameter.
# Uses the same file name and replaces the .sla extension with .pdf
#
# usage:
# scribus -g -py to-pdf.py file.sla

import os
import sys

###################################
#
# check scribus
#
###################################

try:
import scribus
except ImportError:
# if the script does not run inside of scribus, launch scribus with this script as paremeter

filename = []

if (len(sys.argv) == 2) :
print("launching the script for " + sys.argv[1])
filename.append(sys.argv[1])
else:
print("launching the script for each .sla in the directory.")
for file in os.listdir('.'):
filenameSplit = os.path.splitext(file)
if filenameSplit[-1] == '.sla' and filenameSplit[0].find('_autosave_') == -1:
filename.append(file)

for file in filename :
print(file)
os.system('scribus -g -py ' + sys.argv[0] + ' -- ' + file)

sys.exit(1)

###################################
#
# Export PDF
#
###################################

if scribus.haveDoc() :
filename = os.path.splitext(scribus.getDocName())[0]
pdf = scribus.PDFfile()
pdf.file = filename+".pdf"
pdf.save()
else :
print("No file open")

Bash file scripts/run.sh :

#!/bin/bash
echo "command: ${@:1}"
Xvfb :99 -screen 0 1024x768x16 &
scribus "${@:1}"

Dockefile into docker/Dockerfile :

FROM ubuntu

RUN apt-get update && apt-get install -y xvfb software-properties-common

RUN add-apt-repository ppa:ubuntuhandbook1/scribus
RUN apt-get update && apt-get install -y scribus


WORKDIR /scripts
COPY scripts/run.sh .

WORKDIR /data

ENV DISPLAY=99
ENTRYPOINT /scripts/run.sh "$0" "$@"

add to docker-compose.yml file:

services:

...

scribus:
build:
context: .
dockerfile: docker/Dockerfile
volumes:
- .:/data
environment:
- DISPLAY=:99

Now we can run using docker compose run scribus -g -py scripts/to-pdf.py -- generated.sla and it will be generate generated.pdf file.

Conclusions

The open source enable to automate many things with few line of code here we see how to use Scribus to automate publishing but many thing can happen thanks open source so contribute and help it!

--

--

Nicola Landro
Nicola Landro

Written by Nicola Landro

Linux user and Open Source fun. Deep learning PhD. , Full stack web developer, Mobile developer, cloud engineer and Musitian.

No responses yet