Importing addresses from a text file: Difference between revisions

From Scribus Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 42: Line 42:
</pre>
</pre>
===Notes===
===Notes===
* One of the rigid features is that the text file must be named <tt>addr.txt</tt>, but one could easily add a FileDialog to be able to input a filename.
* One of the rigid features is that the text file must be named <tt>addr.txt</tt>, but one could easily add a FileDialog to be able to input a filename. So, before the line:
<pre>
file = open('addr.txt', 'r')
</pre>
you could add this (with or without this filter for files ending in '.txt'):
<pre>
selectfile = scribus.fileDialog("Select Your Address File", "*.txt")
</pre>
then modify the next line:
<pre>
file = open(selectfile, 'r')
</pre>

Revision as of 14:24, 18 February 2007

A question on the mail-list asked about how to import a series of addresses from an external file. One of the ways to do this would be with some kind of database, although for simpler projects, creating the database, then creating the script to use it would be too much work.

This script, importadd.py, uses a simple text file with a particular format, parsing the addresses and then creating a text frame for each. As written, the script opens a new document, then creates new pages. One could modify it to make use of an existing document as well.

The format of the address text file is that it begins immediately with the first address, and the address is laid out as it will be on the page. After the address, there is a single blank line separating it from the next address. This is important because this blank line -- and it must be a blank line and no characters -- is the flag to tell the script that the address is finished, go ahead and make a new page, make a text frame and put the address in it.

Here is the script:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
 File importadd.py
 Created 2007-02-17
 Gregory Pittman
 imports addresses from text file
 puts them in text frame
 adds new pages as needed
"""
import scribus

if scribus.newDoc(scribus.PAPER_LETTER, (10,10,20,20),scribus.PORTRAIT, 1, scribus.UNIT_POINTS, scribus.NOFACINGPAGES, scribus.FIRSTPAGERIGHT):

    file = open('addr.txt', 'r')
    add = ''
    page = 0
    while 1:
        info = file.readline()
        if not info: break
        if info != '\n':
            add = add + info
        else:
            if page:
                scribus.newPage(-1)
            A = scribus.createText(60, 100, 200, 100)
            scribus.setText(add, A)
            scribus.setTextAlignment(scribus.ALIGN_LEFT, A)
            scribus.setFont("Luxi Sans Regular", A)
            scribus.setFontSize(12, A)
            add = ''
            page = page + 1

Notes

  • One of the rigid features is that the text file must be named addr.txt, but one could easily add a FileDialog to be able to input a filename. So, before the line:
file = open('addr.txt', 'r')

you could add this (with or without this filter for files ending in '.txt'):

selectfile = scribus.fileDialog("Select Your Address File", "*.txt")

then modify the next line:

file = open(selectfile, 'r')