Adding 'DRAFT' to a document: Difference between revisions

From Scribus Wiki
Jump to navigation Jump to search
(Script for adding 'DRAFT' to a document)
 
m (Took note of the comment to use 'import scribus')
 
(24 intermediate revisions by 4 users not shown)
Line 1: Line 1:
#!/usr/bin/env python
{{Scripting Index}}
'''
A script to place a light grey watermark 'DRAFT' on a new layer. Requires an existing document, but can be modified to create a new document if it does not exist.
A script to place a light grey watermark 'DRAFT' on a new layer.  
 
Requires an existing document, but can be modified to  
''Note that this script can be run on a Master Page, which allows you to apply this to as many pages as you wish, without needing to run the script again.''
create a new document if it does not exist
 
uses (See the API in Help->Scribus Manual->For Developers->Scripter API;
If it doesn't work for you, it may have something to do with your default font.
  haveDoc
 
  createLayer
[[Image:Draft_example.png|300px]]
  setActiveLayer
 
  createText
Save the following code as [{{#file: draft.py}} draft.py]
  setUnit
 
  setText
<syntaxhighlight lang="python">
  setTextColor
#!/usr/bin/env python
  setFontSize
 
  rotateObject
 
Tested on 1.3.3.12 and A2, A4, A5, Letter
# 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
from scribus import *
# the Free Software Foundation; either version 2 of the License, or
# Could be expanded to include localization here
# (at your option) any later version.
  draft = "DRAFT"
 
#draft = "ENTWURF"
'''
#draft = "BROUILLON"
A script to place a light grey watermark 'DRAFT' on a new layer.  
L = len(draft)                               # The length of the word  
Requires an existing document, but can be modified to  
                                              # will determine the font size
create a new document if it does not exist
defineColor("gray", 11, 11, 11, 11)           # Set your own color here
 
if haveDoc():
uses (See the API in Help->Scribus Manual->For Developers->Scripter API;
    u = getUnit()                             # Get the units of the document
haveDoc
    setUnit(UNIT_MILLIMETERS)                 # Set the document units to mm,                                             
createLayer
    (w,h) = getPageSize()                     # needed to set the text box size
getActiveLayer
    createLayer("c")
setActiveLayer
    setActiveLayer("c")
createText
    T = createText(w/6, 6*h/10 , h, w/2.5)   # Create the text box
setUnit
    setText(draft, T)                         # Insert the text
setText
    setTextColor("gray", T)                   # Set the color of the text
setTextColor
    setFontSize((w/210)*(180 - 10*L), T)     # Set the font size according to length  
setFontSize
                                              # and width of the document
rotateObject
    rotateObject(45, T)                       # Turn it round antclockwise 45 degrees
Tested on 1.4.3 and A2, A4, A5, Letter.
    setUnit(u)                               # return to original document units
'''
import scribus
#from scribus import *
 
# Could be expanded to include localization here
draft = "DRAFT"
#draft = "ENTWURF"
#draft = "BROUILLON"
 
L = len(draft)                               # The length of the word  
                                              # will determine the font size
defineColor("gray", 11, 11, 11, 11)           # Set your own color here
 
if haveDoc():
    u = getUnit()                           # Get the units of the document
    al = getActiveLayer()                    # Identify the working layer
    setUnit(UNIT_MILLIMETERS)                 # Set the document units to mm,                                             
    (w,h) = getPageSize()                     # needed to set the text box size
 
    createLayer("c")
    setActiveLayer("c")
 
#    T = createText(w/6, 6*h/10 , h, w/2)  # Create the box - here is the original line, which
                                          # makes an excessively large frame (see pic above)
    T = createText(w/6, 6*h/10 , 2h/3, w/5) # Create the box - I think you will find this is better
                                            # by making a smaller frame
    setText(draft, T)                         # Insert the text
    setTextColor("gray", T)                 # Set the color of the text
    setFontSize((w/210)*(180 - 10*L), T)     # Set the font size according to length and width
 
    rotateObject(45, T)                     # Turn it round antclockwise 45 degrees
    setUnit(u)                               # return to original document units
    setActiveLayer(al)                      # return to the original active layer
</syntaxhighlight>
 
===A note on style===
The favored way to use a python script in Scribus is to use the command
<syntaxhighlight lang="python">
import scribus
</syntaxhighlight>
instead of
<syntaxhighlight lang="python">
from scribus import *
</syntaxhighlight>
The reason for this is that the latter method imports all of the Scribus python API into the interpreter, rather unnecessary. However, if you use this preferred method, then your Scribus commands need to use a prefix, for example
<syntaxhighlight lang="python">
scribus.setActiveLayer(al)
</syntaxhighlight>
This also applies to Scribus constants, for example '''scribus.UNIT_MILLIMETERS'''. If you're wondering which commands are those which belong to Scribus, generally speaking anything not highlighted in color above is a Scribus command, so '''from''', '''import''', and '''len''' are all basic python commands.

Latest revision as of 23:49, 9 December 2015

This article is part of the Scripts series.

A script to place a light grey watermark 'DRAFT' on a new layer. Requires an existing document, but can be modified to create a new document if it does not exist.

Note that this script can be run on a Master Page, which allows you to apply this to as many pages as you wish, without needing to run the script again.

If it doesn't work for you, it may have something to do with your default font.

Draft example.png

Save the following code as [{{#file: draft.py}} draft.py]

#!/usr/bin/env python


# 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.

'''
A script to place a light grey watermark 'DRAFT' on a new layer. 
Requires an existing document, but can be modified to 
create a new document if it does not exist

uses (See the API in Help->Scribus Manual->For Developers->Scripter API;
 haveDoc
 createLayer
 getActiveLayer
 setActiveLayer
 createText
 setUnit
 setText
 setTextColor
 setFontSize
 rotateObject
Tested on 1.4.3 and A2, A4, A5, Letter. 
'''
import scribus
#from scribus import *

# Could be expanded to include localization here
draft  = "DRAFT"
#draft = "ENTWURF"
#draft = "BROUILLON"

L = len(draft)                                	# The length of the word 
                                              	# will determine the font size
defineColor("gray", 11, 11, 11, 11)           	# Set your own color here

if haveDoc():
    u  = getUnit()                            	# Get the units of the document
    al = getActiveLayer()                     	# Identify the working layer
    setUnit(UNIT_MILLIMETERS)                 	# Set the document units to mm,                                            
    (w,h) = getPageSize()                     	# needed to set the text box size

    createLayer("c")
    setActiveLayer("c")

#    T = createText(w/6, 6*h/10 , h, w/2)  # Create the box - here is the original line, which
                                           # makes an excessively large frame (see pic above)
    T = createText(w/6, 6*h/10 , 2h/3, w/5)  # Create the box - I think you will find this is better
                                             # by making a smaller frame
    setText(draft, T)                         	# Insert the text
    setTextColor("gray", T)                  	# Set the color of the text
    setFontSize((w/210)*(180 - 10*L), T)     	# Set the font size according to length and width

    rotateObject(45, T)                      	# Turn it round antclockwise 45 degrees
    setUnit(u)                               	# return to original document units
    setActiveLayer(al)                       	# return to the original active layer

A note on style

The favored way to use a python script in Scribus is to use the command

import scribus

instead of

from scribus import *

The reason for this is that the latter method imports all of the Scribus python API into the interpreter, rather unnecessary. However, if you use this preferred method, then your Scribus commands need to use a prefix, for example

scribus.setActiveLayer(al)

This also applies to Scribus constants, for example scribus.UNIT_MILLIMETERS. If you're wondering which commands are those which belong to Scribus, generally speaking anything not highlighted in color above is a Scribus command, so from, import, and len are all basic python commands.