Skip to content

Instantly share code, notes, and snippets.

@BaderSZ
Last active July 7, 2023 13:23
Show Gist options
  • Save BaderSZ/364218a65257ae2201f473c750fcc495 to your computer and use it in GitHub Desktop.
Save BaderSZ/364218a65257ae2201f473c750fcc495 to your computer and use it in GitHub Desktop.
Bulk convert a selection of Scribus files to PDF
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# need the following for PyQt linting
# pylint: disable=c-extension-no-member
"""bulk_export_pdf.py
Bulk convert a selection of Scribus files to PDF with the built-in
export preferences. This script must be run from inside scribus with
the Scripts->Execute feature.
Note: This script requires PyQt5, since Scripter does not have (AFAIK)
a built in dialog to select multiple files.
"""
try:
import os
import sys
import errno
from PyQt5 import QtWidgets
import scribus
except ImportError as exp:
scribus.messageBox("Scribus Error", "Scribus Exception! See logs: " +
exp, scribus.ICON_WARNING, scribus.BUTTON_OK)
sys.exit(1)
def export_pdf(input_file: str, output_dir: str, output_fmt):
"""export_pdf
Function to use in loop.
arguments: input_file, output_directory, info dict and a format (default 1)
returns: none
"""
try:
scribus.openDoc(input_file)
except ImportError as mainexp:
scribus.messageBox(
"Import error", "could not import necessary libraries: " + mainexp)
sys.exit(1)
if scribus.haveDoc():
pdf = scribus.PDFfile()
pdf.file = output_dir + "/" + \
os.path.splitext(os.path.basename((input_file)))[0] + ".pdf"
pdf.outdst = output_fmt
pdf.save()
scribus.closeDoc()
else:
scribus.messageBox('Error!', "No input sla given, or could not open file!",
scribus.ICON_WARNING, scribus.BUTTON_OK)
sys.exit(errno.EINVAL)
if __name__ == "__main__":
# Get input files. Qt.getOpenFileNames returns Tuple (files, format). Get first.
# We get an empty list in case dialog is cancelled.
INFILES = QtWidgets.QFileDialog.getOpenFileNames(
None, 'Choose scribus files', "", "Scribus-files (*.sla *.sla.gz);;All files (*)")[0]
if len(INFILES) == 0:
scribus.messageBox("Input error", "No input files given!",
scribus.ICON_WARNING, scribus.BUTTON_OK)
sys.exit(errno.EINVAL)
# Get the output directory
# Empty string in case dialog is cancelled.
OUTDIR = QtWidgets.QFileDialog.getExistingDirectory(
None, "Get Export Directory", "", QtWidgets.QFileDialog.ShowDirsOnly)
if not OUTDIR:
scribus.messageBox("Input error", "No output drirectory given!",
scribus.ICON_WARNING, scribus.BUTTON_OK)
sys.exit(errno.EINVAL)
# Output PDF format, print (1) or digital (0)
# this depends on preset document settings (bleeds, colors, etc.)
output_format_str = scribus.valueDialog(
"Export type", "Digital or Print PDF?\n 0 for digital, 1 for print", "1")
try:
outputfmt = int(output_format_str)
if outputfmt not in [0, 1]:
raise ValueError
except ValueError as outpexp:
scribus.messageBox("Input Error", "Print value invalid! 0 or 1 accepted, " +
output_format_str + "given.\n" + outpexp,
scribus.ICON_WARNING, scribus.BUTTON_OK)
sys.exit(errno.EINVAL)
for slafile in INFILES:
export_pdf(os.path.abspath(slafile), OUTDIR, outputfmt)
scribus.messageBox(
"Scribus export", "Export compelte! Files are in:\n" + OUTDIR, scribus.BUTTON_OK)
sys.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment