Line numbering for text frames: Difference between revisions

From Scribus Wiki
Jump to navigation Jump to search
(Created page with "{{Scripting Index}} Here is an idea that comes from a feature request, specifically http://bugs.scribus.net/view.php?id=1975 As you can see this was filed back in 2005, so i...")
 
Line 68: Line 68:
</syntaxhighlight>
</syntaxhighlight>
===Notes===
===Notes===
At this point, it only works on a single selected text frame, by getting information about the font size, linespacing, number of lines of text, then creates a small text frame just to the left of the original, which if fills with a column of numbers, created with a loop based on the number of lines of text.
Getting back to the original usage envisioned, next I'd like to create a new layer, and put this text frame on that instead of the same layer as the original text. This way, the new layer could be made invisible and nonprintable (and therefore not exporting to PDF).
Finally, the last element would be to run this on all the text frames in a document. This has pros and cons, but at least a starting point for variants.

Revision as of 00:57, 28 November 2015

This article is part of the Scripts series.

Here is an idea that comes from a feature request, specifically http://bugs.scribus.net/view.php?id=1975

As you can see this was filed back in 2005, so it's mostly been gathering dust, or whatever bug reports do over the years. Kunda wondered if this might be scriptable, so I began to see what I could do. Consider this a work in progress.

linenumber.py

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



"""
import scribus

if scribus.haveDoc():
    linecount = 1
    textnumbers = ""
else:
    scribus.messageBox('Usage Error', 'You need a Document open', icon=0, button1=1)
    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)
textbox = scribus.getSelectedObject()
ftype1 = scribus.getObjectType(textbox)
if (ftype1 != "TextFrame"):
    scribus.messageBox('Object Type', "Selected object must be a text frame",
                       scribus.ICON_WARNING, scribus.BUTTON_OK)
    sys.exit(2)
    
frameW, frameH = scribus.getSize(textbox)
textlines = scribus.getTextLines(textbox)
linespace = scribus.getLineSpacing(textbox)
xframe, yframe = scribus.getPosition(textbox)
font_size = scribus.getFontSize(textbox)
#scribus.messageBox("Font Size",str(font_size),icon=scribus.ICON_NONE,button1=scribus.BUTTON_OK)

while (linecount <= textlines):
    textnumbers = textnumbers + str(linecount) + "\n"
    linecount = linecount + 1
newframe = scribus.createText(xframe - font_size * 1.5, yframe, font_size * 1.5, frameH)
scribus.selectObject(newframe)
scribus.insertText(textnumbers, 0, newframe)
scribus.setFontSize(font_size, newframe)
scribus.setLineSpacing(linespace, newframe)
scribus.setLineSpacingMode(1, newframe)

scribus.setRedraw(1)
scribus.docChanged(1)
endmessage = 'Successfully ran script!' # Change this message to your liking
scribus.messageBox("Finished", endmessage,icon=scribus.ICON_NONE,button1=scribus.BUTTON_OK)

Notes

At this point, it only works on a single selected text frame, by getting information about the font size, linespacing, number of lines of text, then creates a small text frame just to the left of the original, which if fills with a column of numbers, created with a loop based on the number of lines of text.

Getting back to the original usage envisioned, next I'd like to create a new layer, and put this text frame on that instead of the same layer as the original text. This way, the new layer could be made invisible and nonprintable (and therefore not exporting to PDF).

Finally, the last element would be to run this on all the text frames in a document. This has pros and cons, but at least a starting point for variants.