Poor man's mail merge

From Scribus Wiki
Revision as of 16:56, 26 August 2009 by Ale (talk | contribs) (New page: <pre> #!/usr/bin/env python # -*- coding: utf-8 -*- """ ABOUT THIS SCRIPT: Import CSV data files as tables into Scribus """ import sys try: # Please do not use 'from scribus import ...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
ABOUT THIS SCRIPT:
Import CSV data files as tables into Scribus
"""


import sys

try:
    # Please do not use 'from scribus import *' . If you must use a 'from import',
    # Do so _after_ the 'import scribus' and only import the names you need, such
    # as commonly used constants.
    import scribus
except ImportError,err:
    print "This Python script is written for the Scribus scripting interface."
    print "It can only be run from within Scribus."
    sys.exit(1)

#########################
# YOUR IMPORTS GO HERE  #
#########################
import csv

def getCSVdata():
    """opens a csv file, reads it in and returns a 2 dimensional list with the data"""
    csvfile = scribus.fileDialog("csv2table :: open file", "*.csv")
    if csvfile != "":
        try:
            reader = csv.reader(file(csvfile))
            datalist=[]
            for row in reader:
                rowlist=[]
                for col in row:
                    rowlist.append(col)
                datalist.append(rowlist)
            return datalist
        except Exception,  e:
            scribus.messageBox("csv2table", "Could not open file %s"%e)
    else:
        sys.exit


def create(argv):
    """
        - create a page using the "badge" master page.
        - add the text field with the name
        - add the picture with the project logo
        - add the text with the role in the project
    """
    #########################
    #  YOUR CODE GOES HERE  #
    #########################
    userdim=scribus.getUnit() #get unit and change it to mm
    scribus.setUnit(scribus.UNIT_POINTS)
    
    data = getCSVdata()
    
    scribus.progressTotal(len(data))
    scribus.setRedraw(False)

    name_x = 20;
    name_y = 140;
    name_w = 250;
    name_h = 30;
    project_x = 20;
    project_y = 165;
    project_w = 250;
    project_h = 45;

    i = 0;

    for row in data:
        #if i > 0 :
        #sys.exit(1)
        # scribus.messageBox("csv2table", row)
        #scribus.messageBox("csv2table", row[0])

        # create a page
        scribus.newPage(-1, 'badge');
        # create and fill the text boxes
        textbox=scribus.createText(name_x, name_y, name_w, name_h)
        scribus.insertText(row[0],0, textbox)
        scribus.setTextColor('orange', textbox);
        scribus.setFont('Nimbus Sans L Bold', textbox);
        scribus.setFontSize(24, textbox);
        scribus.setLineSpacing(28, textbox);
        textbox=scribus.createText(project_x, project_y, project_w, project_h)
        scribus.insertText(row[1]+"\n"+row[2], 0, textbox)
        scribus.setFontSize(14, textbox);
        scribus.setFont('Nimbus Sans L Bold', textbox);
        scribus.setLineSpacing(18, textbox);
        i = i + 1;
        scribus.progressSet(i)
    
    scribus.progressReset()
    scribus.setUnit(userdim) # reset unit to previous value
    scribus.docChanged(True)
    scribus.statusMessage("Done")
    scribus.setRedraw(True)


def main(argv):
    """The main() function disables redrawing, sets a sensible generic
    status bar message, and optionally sets up the progress bar. It then runs
    the main() function. Once everything finishes it cleans up after the create()
    function, making sure everything is sane before the script terminates."""
    try:
        scribus.statusMessage("Importing .csv table...")
        scribus.progressReset()
        create(argv)
    finally:
        # Exit neatly even if the script terminated with an exception,
        # so we leave the progress bar and status bar blank and make sure
        # drawing is enabled.
        if scribus.haveDoc():
            scribus.setRedraw(True)
        scribus.statusMessage("")
        scribus.progressReset()

# This code detects if the script is being run as a script, or imported as a module.
# It only runs main() if being run as a script. This permits you to import your script
# and control it manually for debugging.
if __name__ == '__main__':
    main(sys.argv)