Category:Scripts: Difference between revisions
(→Full functional scripts: Added link to planner script) |
|||
(80 intermediate revisions by 9 users not shown) | |||
Line 1: | Line 1: | ||
[[Category:EN]] [[Category:Categories]] | [[Category:EN]] [[Category:Categories]] | ||
'''Other languages:''' [[:Category:Skrypty_i_wtyczki|Polski (pl)]] [[:Category:Scripts_e_Plugins|Portuguese (pt_BR)]] | '''Other languages:''' [[:Category:Skrypty_i_wtyczki|Polski (pl)]] [[:Category:Scripts_e_Plugins|Portuguese (pt_BR)]] | ||
{{TOC right}} | |||
= | = Intro - a simple example = | ||
Since version 1.6.0 Python 3 is supported by Scribus. Scribus commands may be executed by a script. | |||
As a simple example open from the script menu 'Show console', paste the following code and run the script. | |||
<syntaxhighlight lang='python'> | |||
import scribus | |||
</ | origX = 15 | ||
origY = 25 | |||
i = 0 | |||
boxHeight = 20 | |||
boxWidth = 50 | |||
endValueY = origY + 10 * boxHeight | |||
for y in range(origY,endValueY,boxHeight): | |||
i = i + 1 | |||
myFrame = "box"+str(i) | |||
scribus.createText(origX,y,boxWidth,boxHeight,myFrame) | |||
scribus.setLineColor("Black",myFrame) | |||
scribus.setText(str(i),myFrame) | |||
scribus.setFontSize(24,myFrame) | |||
</syntaxhighlight> | |||
Here | Note that the first line of the code needs to be blank. The units given are in millimeters. This example uses four Scribus commands. | ||
= Scripter = | |||
Here is documentation about the current scripting component in Scribus: | |||
* [[Scripter API]] -- Routines which can be used in scripts to control Scribus | * [[Scripter API]] -- Routines which can be used in scripts to control Scribus | ||
* [[Scripter/Databases| Databases]] -- Scripts can interact with database software like MySQL or SQLite | * [[Scripter/Databases| Databases]] -- Scripts can interact with database software like MySQL or SQLite | ||
This wiki is capable of letting users download raw code embedded into the pages. | This wiki is capable of letting users download raw code embedded into the pages and have it appear syntax highlighted. | ||
* So as to cite python script code with proper syntax hilight, use the following tag <nowiki><syntaxhighlight lang='python'></nowiki> instead of using the usual HTML tag <nowiki><pre></nowiki>. | |||
* See example and other possible tags : [[Raw_Code_Download| Wikitext for Raw Code Download]] | |||
== Pure Python Functions == | == Pure Python Functions == | ||
The open() function is a pure python function and does not involve any | The open() function is a pure python function and does not involve any | ||
Line 28: | Line 45: | ||
names in python: | names in python: | ||
* http://docs.python.org/tutorial/inputoutput.html | |||
* http://www.evanjones.ca/python-utf8.html | |||
* http://boodebr.org/main/python/all-about-python-and-unicode | |||
The string returned by the Scribus fileDialog() function is always Unicode UTF-8 encoded. | The string returned by the Scribus fileDialog() function is always Unicode UTF-8 encoded. | ||
Line 37: | Line 54: | ||
This is up to you to handle python exceptions, Scribus cannot do that in your place | This is up to you to handle python exceptions, Scribus cannot do that in your place | ||
==Script Template== | |||
Here is a '''script template''' with all setups. Use it as a ''starting point'' for writing your own script, fill in your application specific code. | |||
[[Scripter/Snippet/Main| script template main()]]. There is also a template called [[boilerplate.py]] within Scribus Script directory. | |||
You might also check out [[scriptmaker.py]], a Python script to run outside of Scribus to make the beginnings of a script that only runs inside of Scribus! | |||
==[[Troubleshooting Scripts]]== | |||
The content is on another page, since this one has gotten a bit out of control in terms of its size. | |||
== Building blocks / snippets== | |||
This section contains snippets you can use to create your own scripts. | This section contains snippets you can use to create your own scripts. | ||
<div style="border:2px solid lightgrey;padding:2px">ToDo: Add some more snippets</div> | |||
* reading a file | * reading a file | ||
* updating a status bar | * updating a status bar | ||
Line 50: | Line 74: | ||
* pausing the screen update | * pausing the screen update | ||
=== | |||
===Get page parameters=== | |||
[[ | <syntaxhighlight lang='python'> | ||
pageHeight = getPageSize()[0] | |||
pageWidth = getPageSize()[1] | |||
pageMargins = getPageMargins() | |||
marginTop = pageMargins[0] | |||
marginLeft = pageMargins[1] | |||
marginRight = pageMargins[2] | |||
marginBottom = pageMargins[3] | |||
printableAreaHeight = pageHeight - marginTop - marginBottom | |||
printableAreaWidth = pageWidth - marginLeft - marginRight | |||
print("pageHeight=",pageHeight) | |||
print("pageWidth=",pageWidth) | |||
print("marginTop=",marginTop) | |||
print("marginLeft=",marginLeft) | |||
print("marginRight=",marginRight) | |||
print("printableAreaHeight=",printableAreaHeight) | |||
print("printableAreaWidth=",printableAreaWidth) | |||
</syntaxhighlight> | |||
===Iterate over all elements on a page=== | ===Iterate over all elements on a page=== | ||
< | <syntaxhighlight lang='python'> | ||
page = 1 | page = 1 | ||
pagenum = scribus.pageCount() | pagenum = scribus.pageCount() | ||
Line 65: | Line 109: | ||
// do something | // do something | ||
page += 1 | page += 1 | ||
</ | </syntaxhighlight> | ||
===Get content like text from a frame=== | ===Get content like text from a frame=== | ||
< | <syntaxhighlight lang='python'> | ||
content = [] | content = [] | ||
d = scribus.getPageItems() | d = scribus.getPageItems() | ||
Line 79: | Line 123: | ||
elif (item[1] == 2): | elif (item[1] == 2): | ||
imgname = scribus.getImageFile(item[0]) | imgname = scribus.getImageFile(item[0]) | ||
</ | </syntaxhighlight> | ||
ITEM TYPE | ITEM TYPE | ||
Line 92: | Line 136: | ||
* LatexFrame = 9, | * LatexFrame = 9, | ||
* Multiple = 99 | * Multiple = 99 | ||
=== Get some text frame page number=== | |||
Parsing the document's .sla file (which is just XML), here's the baseic gist to get the page number for a frame named "foo": | |||
<syntaxhighlight lang='python'> | |||
import xml.etree.ElementTree as ET | |||
tree = ET.parse(scribus.getDocName()) # in a real script | |||
# tree = ET.parse(getDocName()) # in the console | |||
root = tree.getroot() | |||
frame = "foo" | |||
element = root.find(f'./DOCUMENT/PAGEOBJECT[@ANNAME="{frame}"]') | |||
page_number = element.get("OwnPage") | |||
</syntaxhighlight> | |||
For this to work, the object has to manually have been given a name, not just leaving the auto-generated one, otherwise it's not stored in the .sla | |||
file. | |||
===Auto-Output all Scripter Commands=== | ===Auto-Output all Scripter Commands=== | ||
From Scribus User http://meiradarocha.jor.br Mailinglist Feb. 2012 | From Scribus User http://meiradarocha.jor.br Mailinglist Feb. 2012 | ||
< | This script needs to be updated for 1.6.n. | ||
The output for this script is collected in [[Automatic Scripter Commands list]]. | |||
<syntaxhighlight lang='python'> | |||
d = dir(scribus) | d = dir(scribus) | ||
for j in d: | for j in d: | ||
Line 114: | Line 179: | ||
exec 'print repr('+j+')\n' | exec 'print repr('+j+')\n' | ||
else: | else: | ||
print '\ | print '\nFUNCTION:\n'+j+'\n\nSYNTAX:' | ||
print res | print res | ||
except: pass | except: pass | ||
</ | </syntaxhighlight> | ||
==Image Manipulation== | ==Image Manipulation== | ||
Line 123: | Line 188: | ||
* [[Scripter/Images| Objects: Images]] -- Example Scripts that manipulate images in a Scribus document | * [[Scripter/Images| Objects: Images]] -- Example Scripts that manipulate images in a Scribus document | ||
==Beginners Scripts== | ==[[Beginners Scripts]]== | ||
More than 30 scripts have been moved to a separate page to reduce the size of this one: [[Beginners Scripts]] | |||
==Basic scripts== | ==Basic scripts== | ||
Line 286: | Line 198: | ||
* [[Enlarge2Page]] - Enlarge an object to the size of the page. | * [[Enlarge2Page]] - Enlarge an object to the size of the page. | ||
* [[Making Guides at an Object's Borders]] | * [[Making Guides at an Object's Borders]] | ||
* [[Using the new applyMasterPage() command]] | |||
* [[Swap Images]] | |||
* [[Creating Text Frames for Image Captions]] | |||
* [[Horizontal Rule over Text Frame]] | |||
* [[Creating an Object-sized Document]] | |||
* [[Translation helper]] | |||
==Full functional scripts== | ==Full functional scripts== | ||
Scripts that are ready for achieving specific tasks | Scripts that are ready for achieving specific tasks | ||
{| | |||
| | |||
* [[Export all text|Export all text]] from a Scribus file | * [[Export all text|Export all text]] from a Scribus file | ||
* [[Export list of frames and page numbers]] | |||
* [[Script Resize selected objects|Resize selected objects]] | * [[Script Resize selected objects|Resize selected objects]] | ||
* [[formatxml|Import XML and apply styles]] | * [[formatxml|Import XML and apply styles]] | ||
Line 298: | Line 218: | ||
* [[Bullets_and_numbered_lists|Bullets and numbered lists]] | * [[Bullets_and_numbered_lists|Bullets and numbered lists]] | ||
* [[Scribus_Generator|Scribus Generator]] Extend Scribus with Mail Merge functionality | * [[Scribus_Generator|Scribus Generator]] Extend Scribus with Mail Merge functionality | ||
| | |||
* [[Calendar]] | |||
* [[CalendarWizard]] | |||
* [[Year-Calendar-Script]] | |||
* [[Planner]] | |||
* [[Image crop, resize and CMYK conversion. Save and reload in TIFF format]] | * [[Image crop, resize and CMYK conversion. Save and reload in TIFF format]] | ||
* [[Image crop, resize and color conversion GUI]] | * [[Image crop, resize and color conversion GUI]] | ||
* [[Apply basic ligatures to a document]] | * [[Apply basic ligatures to a document]] | ||
* [[Import CSV Data]] | * [[Import CSV Data]] | ||
* [[Automatic story formatting]] | |||
* [[Clean-up the imported text based on the Slovak typographic rules]] | |||
* [[Import any Pandoc-readable format]] | |||
|} | |||
==Scripting new Scribus' functions== | ==Scripting new Scribus' functions== | ||
Line 308: | Line 237: | ||
* [[Adjust a text frame to fit its content|Adjust the text frame size according to various factors]] | * [[Adjust a text frame to fit its content|Adjust the text frame size according to various factors]] | ||
* [https://github.com/JLuc/scribus-project-manager Project manager] : create a book out of several parts documents, manage common format and styles, search across parts... | |||
== New scripter engine == | |||
A new script engine is currently being integrated into Scribus. | |||
A plan how the new API could look like is here: https://scribus-scripter.readthedocs.io/en/latest/ (referring to version 1.5). | |||
The next steps will be to get the script engine to work. This has not been made active as of Scribus version 1.6.4. | |||
== Extension scripts and PyQt == | == Extension scripts and PyQt == | ||
* [[Extension script discussion]] | * [[Extension script discussion]] (outdated) | ||
* [[Experimental PyQt projects]] | * [[Experimental PyQt projects]] (outdated) | ||
== Python issues == | == Python issues == | ||
* [[Known Scripter Issues]] | * [[Known Scripter Issues]] (outdated) | ||
= Other = | = Other = | ||
* [ | * Alessandro Rimoldi's script repository [https://github.com/aoloe/scribus-script-repository] | ||
* [[Web optimised PDF]] — learn how to minimize PDF size, make your life easier with the included Perl script (runs outside of Scribus) | * [[Web optimised PDF]] — learn how to minimize PDF size, make your life easier with the included Perl script (runs outside of Scribus) | ||
* [[Imagemagick Imposition]] – a bash script doing simple imposition of PNG files exported from Scribus | * [[Imagemagick Imposition]] – a bash script doing simple imposition of PNG files exported from Scribus | ||
* [[Printing_4-up_tickets_to_hard_copy_printer]] Shows a) automatic numbering in text boxes and b) hard-copy printing from script | * [[Printing_4-up_tickets_to_hard_copy_printer]] Shows a) automatic numbering in text boxes and b) hard-copy printing from script | ||
* [[Using createParagraphStyle]] | * [[Using createParagraphStyle]] | ||
* [[Text and Text Manipulation]] | |||
* Alessandro Rimoldi's blog series on scripter functions - [http://www.graphicslab.org/Blog/Blog a_l_e's blog] (currently being rebuilt) | |||
* [[Windows Full Python Integration]] |
Latest revision as of 11:43, 7 August 2025
Other languages: Polski (pl) Portuguese (pt_BR)
Intro - a simple example
Since version 1.6.0 Python 3 is supported by Scribus. Scribus commands may be executed by a script.
As a simple example open from the script menu 'Show console', paste the following code and run the script.
import scribus
origX = 15
origY = 25
i = 0
boxHeight = 20
boxWidth = 50
endValueY = origY + 10 * boxHeight
for y in range(origY,endValueY,boxHeight):
i = i + 1
myFrame = "box"+str(i)
scribus.createText(origX,y,boxWidth,boxHeight,myFrame)
scribus.setLineColor("Black",myFrame)
scribus.setText(str(i),myFrame)
scribus.setFontSize(24,myFrame)
Note that the first line of the code needs to be blank. The units given are in millimeters. This example uses four Scribus commands.
Scripter
Here is documentation about the current scripting component in Scribus:
- Scripter API -- Routines which can be used in scripts to control Scribus
- Databases -- Scripts can interact with database software like MySQL or SQLite
This wiki is capable of letting users download raw code embedded into the pages and have it appear syntax highlighted.
- So as to cite python script code with proper syntax hilight, use the following tag <syntaxhighlight lang='python'> instead of using the usual HTML tag <pre>.
- See example and other possible tags : Wikitext for Raw Code Download
Pure Python Functions
The open() function is a pure python function and does not involve any Scribus code. Here are some pages with advices on how to handle unicode file names in python:
- http://docs.python.org/tutorial/inputoutput.html
- http://www.evanjones.ca/python-utf8.html
- http://boodebr.org/main/python/all-about-python-and-unicode
The string returned by the Scribus fileDialog() function is always Unicode UTF-8 encoded.
If file is not closed after python error within Scribus: This is up to you to handle python exceptions, Scribus cannot do that in your place
Script Template
Here is a script template with all setups. Use it as a starting point for writing your own script, fill in your application specific code. script template main(). There is also a template called boilerplate.py within Scribus Script directory.
You might also check out scriptmaker.py, a Python script to run outside of Scribus to make the beginnings of a script that only runs inside of Scribus!
Troubleshooting Scripts
The content is on another page, since this one has gotten a bit out of control in terms of its size.
Building blocks / snippets
This section contains snippets you can use to create your own scripts.
- reading a file
- updating a status bar
- selecting a frame
- pausing the screen update
Get page parameters
pageHeight = getPageSize()[0]
pageWidth = getPageSize()[1]
pageMargins = getPageMargins()
marginTop = pageMargins[0]
marginLeft = pageMargins[1]
marginRight = pageMargins[2]
marginBottom = pageMargins[3]
printableAreaHeight = pageHeight - marginTop - marginBottom
printableAreaWidth = pageWidth - marginLeft - marginRight
print("pageHeight=",pageHeight)
print("pageWidth=",pageWidth)
print("marginTop=",marginTop)
print("marginLeft=",marginLeft)
print("marginRight=",marginRight)
print("printableAreaHeight=",printableAreaHeight)
print("printableAreaWidth=",printableAreaWidth)
Iterate over all elements on a page
page = 1
pagenum = scribus.pageCount()
content = []
while (page <= pagenum):
scribus.gotoPage(page)
d = scribus.getPageItems()
for item in d:
// do something
page += 1
Get content like text from a frame
content = []
d = scribus.getPageItems()
for item in d:
if (item[1] == 4):
contents = scribus.getAllText(item[0])
if (contents in content):
contents = 'Duplication, perhaps linked-to frame'
content.append(contents)
elif (item[1] == 2):
imgname = scribus.getImageFile(item[0])
ITEM TYPE
- ItemType1 = 1,
- ImageFrame = 2,
- ItemType3 = 3,
- TextFrame = 4,
- Line = 5,
- Polygon = 6,
- PolyLine = 7,
- PathText = 8,
- LatexFrame = 9,
- Multiple = 99
Get some text frame page number
Parsing the document's .sla file (which is just XML), here's the baseic gist to get the page number for a frame named "foo":
import xml.etree.ElementTree as ET
tree = ET.parse(scribus.getDocName()) # in a real script
# tree = ET.parse(getDocName()) # in the console
root = tree.getroot()
frame = "foo"
element = root.find(f'./DOCUMENT/PAGEOBJECT[@ANNAME="{frame}"]')
page_number = element.get("OwnPage")
For this to work, the object has to manually have been given a name, not just leaving the auto-generated one, otherwise it's not stored in the .sla file.
Auto-Output all Scripter Commands
From Scribus User http://meiradarocha.jor.br Mailinglist Feb. 2012 This script needs to be updated for 1.6.n. The output for this script is collected in Automatic Scripter Commands list.
d = dir(scribus)
for j in d:
try:
exec 'res = '+j+'.__doc__'
if res[0:5] == 'float':
print '\nCONSTANT:\n',j,'\nVALUE: float'
exec 'print '+j+'\n'
elif res[0:5] == 'int(x':
print '\nCONSTANT:\n',j,'\nVALUE: integer'
exec 'print '+j+'\n'
elif res[0:5] == 'tuple':
print '\nTUPLE:\n',j,'\nVALUE:'
exec 'print repr('+j+')\n'
elif res[0:4] == 'str(':
print '\nSTRING:\n',j,'\nVALUE:'
exec 'print repr('+j+')\n'
else:
print '\nFUNCTION:\n'+j+'\n\nSYNTAX:'
print res
except: pass
Image Manipulation
- Objects: Images -- Example Scripts that manipulate images in a Scribus document
Beginners Scripts
More than 30 scripts have been moved to a separate page to reduce the size of this one: Beginners Scripts
Basic scripts
Scripts which give you ideas how you can solve your tasks.
- Poor man's mail merge
- Enlarge2Page - Enlarge an object to the size of the page.
- Making Guides at an Object's Borders
- Using the new applyMasterPage() command
- Swap Images
- Creating Text Frames for Image Captions
- Horizontal Rule over Text Frame
- Creating an Object-sized Document
- Translation helper
Full functional scripts
Scripts that are ready for achieving specific tasks
|
|
Scripting new Scribus' functions
Scripts which sketch new features which may be included in future releases of Scribus
- Project manager : create a book out of several parts documents, manage common format and styles, search across parts...
New scripter engine
A new script engine is currently being integrated into Scribus. A plan how the new API could look like is here: https://scribus-scripter.readthedocs.io/en/latest/ (referring to version 1.5). The next steps will be to get the script engine to work. This has not been made active as of Scribus version 1.6.4.
Extension scripts and PyQt
- Extension script discussion (outdated)
- Experimental PyQt projects (outdated)
Python issues
- Known Scripter Issues (outdated)
Other
- Alessandro Rimoldi's script repository [1]
- Web optimised PDF — learn how to minimize PDF size, make your life easier with the included Perl script (runs outside of Scribus)
- Imagemagick Imposition – a bash script doing simple imposition of PNG files exported from Scribus
- Printing_4-up_tickets_to_hard_copy_printer Shows a) automatic numbering in text boxes and b) hard-copy printing from script
- Using createParagraphStyle
- Text and Text Manipulation
- Alessandro Rimoldi's blog series on scripter functions - a_l_e's blog (currently being rebuilt)
- Windows Full Python Integration
Pages in category "Scripts"
The following 102 pages are in this category, out of 102 total.
A
- A Standard Form with Barcodes and Custom Entries
- Adding 'DRAFT' to a document
- Adjust a text frame to fit its content
- Advanced Graphing
- Align an Image in its Frame
- Align to left page margin
- Apply basic ligatures to a document
- Applying a Frame Style
- Automatic import of images from a directory using a script
- Automatic import of images: Versions not requiring Tkinter
- Automatic story formatting
- Automatically Creating a Graph
- Autoquote2
B
C
- Centering text vertically in a frame
- Clean-up the imported text based on the Slovak typographic rules
- Color Chart
- Convert all colors to CMYK
- Convert RGB imported colors to CMYK
- Convert RGB value to Hex
- Convert Typewriter Quotes to Typographic Quotes
- CopyObject() and pasteObject()
- Create tables out of csv data
- Creating a Graph, Part 2
- Creating a TOC with Scripter
- Creating Markers - another version
- Creating Markers for positioning and cutting
D
E
G
H
I
- Image crop, resize and CMYK conversion. Save and reload in TIFF format
- Image crop, resize and color conversion GUI
- Image Wizard Advanced
- Image Wizard: Scale and Align an Image
- Imagemagick Imposition
- Import an image in the way office-programs do
- Import any Pandoc-readable format
- Import CSV Data
- Importing addresses from a text file
- Infobox in column
M
P
S
- Scale an Image to Fill a Frame Proportionally
- Scrambling Text
- Scribus Generator
- Scribus xhtml using Scripter
- Scribus XML using Scripter
- Script Resize selected objects
- Scripter API
- Scripter Security
- Scripter/Databases
- Scripter/Images
- Scripter/Snippet/Main
- Setting Text Distances from a Script
- Shifting All Page Objects
- Split Image Across Gutter