Create aBibtex file via MySQL and Python

My sources are saved in a MySQL database. I’d like to write my text in LaTeX via LyX. I cannot use the database directly in my reports. So I need a compiler, that will convert my data to a format, that LyX & friends can handle.

Therefore I have made a little compiler in Python. I’ll use the Python mysql.connector. Ubuntu has the relevant packages in the repos.

The compiler will format an ascii-file according to the Bibtex format:

author=”Raielo, Eike J.”,
publisher=”Hovedopgave, EAAA”,
title=”Development of WordPress plugin Dreamgrow Scroll Triggered Box”,

Here is the bibtex compiler:

# kompile to the bibtex format
for row in cursor:
print "@book{wp_" + str(row[3]) + ","
print "author=\"" + row[0] + "\","
print "publisher=\"" + row[4] + "\","
print "title=\"" + row[1] + "\","
print "year=" + str(row[2])
print "}"
print ""

A book should have a unique name. The unique id from the table could be used here. The book will get an id like wp_123 in the line:

print “@book{wp_” + str(row[3]) + “,”

Save the Pyhon file as Create the bibliography as below:

# python > bibliography.bib

The complete code is available on Githup.

International characters may look odd. A workaround is to open the .bib file in KBibTex and save it again. Perhaps it’s not elegant, but it works all right. KBibTex will add special characters. My name will demonstrate what happens to Scandinavian characters:

author = “Jensen, Per Thykj{\ae}r”,
publisher = “Per Thykj{\ae}r Jensen”,
title = “{}”,
year = 2016


By Per Thykjaer Jensen

Per is the WordPress expert at Business Academy Aarhus. Senior Lecturer at the Multimedia Design Programme. His background is in the Arts: MA Literature History and BA Art History. Later he added a MA in IT, interaction and Multimedia. And he is a wicked Linux user.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.