[Scribus] MySQL / Python connection script question; attn: subik

Andy Graybeal andy.graybeal
Mon Feb 4 20:49:11 CET 2008


Petr, I need a breakdown...  I don't understand what you said.

>
> Example "workflow":
>
> Create a basic template document with predefined styles.
>

Does this mean: "create a text box, and give it a style?"  I've just made
a style named "location", it happens to be the same style as the default
one in the style editor.  I've selected the properties of the text box and
then under the text menu, I've assigned the "location" style to this box.

>
> Run a script in Scribus. This script will ask for date and location of
the restaurant. (Create a dialog with date entry and e.g. combobox with
> loications). Then perform SELECT * FROM menu WHERE location = :location
AND active_from :date etc.
>

Do you mean, create a Python script that asks for the Date and Location?

I do however understand the select statement. It says, grab everything out
of the "menu" table where location is such from dialogue box and date is
such from the dialogue box.  I am guessing that :location and :date are
variables... in python?

> Then take results in a loop and paste text into frame with predefined
> styles.

I think i understand this. I've taken things out of databases with PHP and
put them into websites.  Obviously though with Python though.


Let me setup an example:
server: localhost
db: scribe
user: andy
passwd: password
a table like this: CREATE TABLE pet (name VARCHAR(20), owner VARCHAR(20),
species VARCHAR(20), sex CHAR(1), birth DATE, death DATE);
data like this: INSERT INTO pet VALUES
('Puffball','Diane','hamster','f','1999-03-30',NULL);

On our scribus document let's have the names of all the animals owned by
Diane:
SELECT name FROM pet WHERE owner = 'Diane';

>From your example you had :location and :date... so I'm betting some of
that magic would have to be in that SELECT statement.
The result will be that somewhere in the page is going to be the name
"Puffball".

What exactly would my script look like, and what would my scribus document
look like to achieve this?  Do I need styles, or are styles there just to
help me with the formatting and how it looks?

The outcome should be a page with the word "Puffball" somewhere on it right?

I've failed at using the script that comes with Scribus so far... and I
think I have everything set up correctly.  From what I read in the
comments, it reads my database table names (show tables) and then sends
the table names over to a brand new scribus document, and in my example
would place the name "pet" somewhere on that document; I haven't gotten
this to work :)

I'm pretty sure I have the MySQL/Python hooks installed with my package
manager.
---


#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""OK. This is a sample Scribus database connection with Python HOWTO.

DESCRIPTION:
I've got questions about DB and Scribus in my personal mailbox 3-4
times in a month. So this is an common answer for asking people.

Even through I'm an Oracle user/developer I choose MySQL for this
example, because of its presence in the Linux distributions and
hosting availability too.
But the DB server doesn't matter due the PEP 249 - standard DB interface
(http://www.python.org/peps/pep-0249.html).
There are various modules for database accessing:
http://www.python.org/topics/database/modules.html

Anyway - this script provides connection to the database server by the
specified values in the hostname, dbname, username, and password variables.
Then it checks the system for table names in the specified databases
and it displays it in the new document finally. Easy and understandable.


CONTACT:
email : petr;at;yarpen.cz


LICENSE:
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.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
"""

import sys

# environment checking
try:
    import scribus
except ImportError:
    print "This script only runs from within Scribus."
    sys.exit(1)

try:
    import MySQLdb
except ImportError:
    print "You must have 'MySQLdb' installed."
    sys.exit(1)


# connection parameters
hostname = 'localhost'
dbname = 'scribe'
username = 'andy'
password = 'password'

# connection to the network wide server would be time consuming. So get
the hint to the user.
scribus.statusMessage('Connecting to the ' + hostname + ' server. Be
patient, please...')

# Database related issues
try:
    conn = MySQLdb.connect(passwd=password, db=dbname, host=hostname,
user=username)
except:
	scribus.messageBox('DB connection example', 'Connection error. You should
specify your login in the script')
	sys.exit(1)

cur = conn.cursor()
# get the list of the databases
# it's like 'select * from dba_tables' in Oracle
count = cur.execute('show tables')
# formating the output
result = str(count) + ' table(s) in the ' + dbname + ' database.\n\n'
for i in cur.fetchall():
    result = result + i[0] + '\n'

# Scribus presentation part
scribus.newDoc(scribus.PAPER_A5, (10, 10, 20, 20), scribus.PORTRAIT, 1,
scribus.UNIT_POINTS, scribus.NOFACINGPAGES, scribus.FIRSTPAGERIGHT)
txtName = scribus.createText(10, 10, 200, 200)
scribus.setText(result, txtName)

scribus.statusMessage('Script done.')

---

-Andy




More information about the scribus mailing list