Skip to content

Code examples (Python)

William Bain edited this page Jun 22, 2015 · 3 revisions

Some code examples to illustrate how to use the Libmei Python bindings:

Reading in an MEI XML file

import pymei

# An optional boolean second argument to documentFromFile controls
# validation strictness. The default is True.
doc = pymei.documentFromFile("beethoven.mei").getMeiDocument()

Writing out an XML file

from pymei import MeiDocument, MeiElement, documentToFile

# create a very basic document structure
doc = MeiDocument()
root = MeiElement("mei")
root.id = "myid"
doc.root = root

note = MeiElement("note")
note.id = "noteid"
note.value = "value"
note.tail = "tail"
root.addChild(note)

# This will return True if it was successful
status = documentToFile(doc, 'testdoc.mei')

You can also look at the Python unit tests code for more examples.