aoloe / scribus-script-repository

Repository with python scripts from the Scribus community
36 stars 11 forks source link

Merge script #31

Closed S1SYPHOS closed 5 years ago

S1SYPHOS commented 5 years ago

Hello again, I just wondered, after looking through the forum, if there was an easy way to merge .sla files conveniently (eg, using a python script).

Cheers, S1SYPHOS

aoloe commented 5 years ago

do you mean: add pages from a .sla to a different .sla?

i'm not sure if the scripter API allows, it but, yes, since scribus can do that, there is no reason why the script should not be able to do so.

P.S.: ... i've checked in the API help, there is: "Page commands > importPage". the documentation is a bit succinct, but it seems to have the features needed for merging two .sla...

S1SYPHOS commented 5 years ago

Hello again, sadly, I don't have the time right now to dive into this, but will do at some point. Thanks for your help so far!

Cheers S1SYPHOS

S1SYPHOS commented 5 years ago

Based on the link provided below (the API help, as you called it), I wrote this script which should be working:

##
# Imports all pages of an SLA file into another one
#
# For more information, see https://wiki.scribus.net/canvas/Automatic_Scripter_Commands_list
#
# Usage:
# scribus -g -py import-pages.py base_file.sla import_file.sla --page INT (optionally: --before)
#
# License: MIT
# (c) Martin Folkers
##

import scribus
import argparse

parser = argparse.ArgumentParser(
    description='Imports all pages of an SLA file into another one'
)

parser.add_argument(
    'files',
    nargs='*',
    default=None,
    help='SLA files to be processed',
)

parser.add_argument(
    '--page',
    type=int,
    help='Pages are imported before / after this page'
)

parser.add_argument(
    '--before',
    action='store_true',
    help='Imports pages before instead of after'
)

args = parser.parse_args()

def list_sla_pages(sla_file):
    scribus.openDoc(sla_file)
    page_count = scribus.pageCount()

    l = []
    for i in range(1, page_count + 1):
        l.append(i)

    t = tuple(l)
    scribus.closeDoc()

    return t

sla_files = args.files

base_file = sla_files[0]
import_file = sla_files[1]

at_page = args.page
where = 0 if args.before == True else 1 # 0 = before; 1 = after

scribus.openDoc(base_file)
scribus.importPage(import_file, list_sla_pages(import_file), 1, at_page, where)
scribus.saveDoc()
scribus.closeDoc()

Feedback and criticism, as always, much appreciated!

aoloe commented 5 years ago

looks good!

a good example of a script that can do a specific task from the command line...

only one hint: you could use even more explicit variable names:

i would also avoid creating sla_files and directly assign the values of arg.files to base_file and import_file.

all in all, the script is short enough, that one could basically use any name...

one more thing concerning the list_sla_pages function... i wonder if you could not just result = range(1, scribus.pageCount() + 1) (you will need to assign the result to a temporary variable, since you need to close the scribus file before exiting the function)

S1SYPHOS commented 5 years ago

I took your considerations into account, and might have stumbled upon something else: When testing my script, I wondered why pages often were created at the end of the document, not (as I had specified) before or after a certain page - it's because the last two parameters seem to be interchanged.

It should rather be like this:

FUNCTION:
importPage

SYNTAX:
importPage('fromDoc', (pageTuple), [create, importWhere, importWherePage])
Imports a set of pages (given as a tuple) from an existing document (the file name must be given). This functions maps the "Page->Import" dropdown menu function.

- fromDoc: *string*; the filename of the document to import pages from
- pageList: *tuple*; with page numbers of pages to import
- create: *integer*; 1 to insert new pages (default), 0 to replace existing pages
- importWhere: *integer*; only used when `create` = 1;
  - 2 to create pages at the end of the document
  - 1 to create pages after `importWherePage`
  - 0 to create pages before `importWherePage`
- importWherePage: *integer*; the page number (of the current document) at which import the pages

Well, that's something - I'll have a :cookie: and send a PR, if that's alright with you.

S1SYPHOS commented 5 years ago

.. aaaaand there it is! :martial_arts_uniform: