from xml.dom.minidom import DOMImplementation
imp = DOMImplementation()
doctype = imp.createDocumentType(qualifiedName='foo', publicId='', systemId
    ='http://www.path.to.my.dtd.com/my.dtd')
doc = imp.createDocument(None, 'foo', doctype)
doc.toxml()
Set a DTD using minidom in python