Creating an Object-sized Document

From Scribus Wiki
Jump to: navigation, search
This article is part of the Scripts series.

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().

object2doc.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# File: object2doc.py - 
# © 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.
"""
USAGE

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():
     pass
else:
    scribus.messageBox('Usage Error', 'You need a Document open', scribus.ICON_WARNING, scribus.BUTTON_OK)
    sys.exit(2)

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)
    sys.exit(2)
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)
    sys.exit(2)
sel_obj = scribus.getSelectedObject()
scribus.copyObject(sel_obj)
units = scribus.getUnit()
scribus.setUnit(scribus.UNIT_POINTS)
objwidth, objheight = scribus.getSize(sel_obj)
scribus.setUnit(units)
scribus.newDocument((objwidth,objheight), (10,10,10,10),scribus.PORTRAIT,1,scribus.UNIT_POINTS,scribus.PAGE_1,0,1)
scribus.pasteObject(sel_obj)
scribus.moveObjectAbs(0,0)
scribus.setRedraw(1)
scribus.docChanged(1)
scribus.messageBox("Finished", "Done!",scribus.ICON_NONE,scribus.BUTTON_OK)

Notes

  • 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 measurements that come from getSize() and the outer boundaries of the line, so in other words, the document size ended up a bit smaller than the line, even though all the control points of the line were on the page. This is especially noticeable when the stroke width is thick. The same would apply to a shape or polygon with a thick stroke.
  • If you run this on a rotated object, a document page will be created that is not rotated, so placement of the rotated copy will be off, though correctable by rotating the object to 0°. Also related to this is that this script assumes a Basepoint in the upper left-hand corner of the object, so that the upper left-hand corner of the object ends up in the upper left hand corner of the page.