Importing addresses from a text file

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, or not only place these addresses, but also other page elements as you go, such as the mass mailing envisioned by the List questioner.

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: """ File importadd.py Created 2007-02-17 Gregory Pittman imports addresses from text file puts them in text frame adds new pages as needed The format for addr.txt is a plain text file beginning with the first address, one line of the file for each line of the address. No blank lines inside the address, and a single blank line between the addresses and *two* blank lines after the last address. """ import scribus
 * 1) !/usr/bin/env python
 * 2) -*- coding: utf-8 -*-

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

An Example Address File
Monsieur Jean Durand 25 Rue des Fleurs 33500 LIBOURNE FRANCE
 * Once again, the file should contain nothing other than the addresses. The blank line between addresses has no other characters -- it needs to satisfy this if info != '\n' test (technically, needs to fail the test) in order to recognize that the address has finished. For this reason, there also needs to be two blank lines at the end, and there can be no blank lines in the middle of the address. Note the need for two blank lines at the end -- perhaps due to the way that Python reads EOF.
 * The addresses came from a website showing formats for postal addresses to various countries, so as far as I know do not identify any living persons.

Sr. D. Alvaro Blanco Ruiz Luna 10 - 3° 28300 ARANJUEZ (MADRID) SPAIN

John Smith 100 MAIN ST PO BOX 1022 SEATTLE WA 98104 USA

Herrn Ebelhard Wellhausen Wittekindshof Schulstrasse 4 32547 Bad Oyenhausen GERMANY

Sig. Mario Rossi Viale Europa, 22 00144 ROMA, RM ITALY

M. Andrée TROMMER BP 5019 L-1050 Luxembourg LUXEMBOURG

Mr. Walter C. Brown 49 Featherstone Street LONDON EC1Y 8SY ENGLAND