Creating an Object-sized Document

From Scribus Wiki
Revision as of 14:28, 29 March 2017 by Gpittman (talk | contribs) (→‎Notes)
Jump to navigation Jump to search

Here is a small script based on a request from the mail list. What the poster wanted to know was whether there was a way to export a graphics frame to a file of some sort. Although you can export a document page as an image, this then requires some editing afterward. You could also use some type of screen grab software.

Another method would be to make a copy of the original object, create a new document the same size as the object, and then paste the object to it. This way, you could then either export to PDF or export as an image, with no need to trim the image later.

Here is a script that does this automatically, following these above steps, using copyObject() and pasteObject().

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# File: - 
# © 2017.03.28 Gregory Pittman
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

You must have a document open, and an object selected.

The script copies the object, then creates a new single-page

document having the width and height of the selected object.

It then pastes the object to the new document, and moves it to 

the 0,0 position.

import scribus

if scribus.haveDoc():
    c = 0
    scribus.messageBox('Usage Error', 'You need a Document open', icon=0, button1=1)

if scribus.selectionCount() == 0:
    scribus.messageBox('Scribus - Usage Error',
        "There is no object selected.\nPlease select a text frame and try again.",
        scribus.ICON_WARNING, scribus.BUTTON_OK)
if scribus.selectionCount() > 1:
    scribus.messageBox('Scribus - Usage Error',
        "You have more than one object selected.\nPlease select one text frame and try again.", scribus.ICON_WARNING, scribus.BUTTON_OK)
sel_obj = scribus.getSelectedObject()
units = scribus.getUnit()
objwidth, objheight = scribus.getSize(sel_obj)
scribus.newDocument((objwidth,objheight), (10,10,10,10),scribus.PORTRAIT,1,scribus.UNIT_POINTS,scribus.PAGE_1,0,1)
scribus.messageBox("Finished", "Done!",scribus.ICON_NONE,scribus.BUTTON_OK)


Incidentally, this seems to work not only on image frames, but any other sort of frame, including in imported SVG. It also works for shapes and polygons, render frames or Bezier curves, for example. I notice that with Bezier curves, there is a slight mismatch between the bounding box and the measurements that come from sizeObject(), so in other words, the document size ended up a bit smaller than the bounding box.