A small python script

Recently I changed from using Secrets on my Android phone to using KeePassDroid for storing my passwords. The point of this change was that KeePass is also available for Linux (called KeePassX) and I can now sync my passwords between my desktop and my phone using Dropbox.

However I had to migrate my passwords from Secrets to KeePassX. Luckily Secrets can export to CSV and KeePassX can import XML. So Python to the rescue.

Here is the small Python program I made in case anyone on the Internet should be interested (does anyone know of a nice code syntax highlighter plugin for wordpress?):

import csv
from xml.dom.minidom import Document

doc = Document()
db = doc.createElement("database")
doc.appendChild(db)

group = doc.createElement("group")
db.appendChild(group)

gtitle = doc.createElement("title")
gtitle.appendChild(doc.createTextNode("Imported from Secrets"))
group.appendChild(gtitle)


row_reader = csv.reader(open("secrets.csv", "rb"))
row_reader.next() # skip the header
for row in row_reader:
 e = doc.createElement("entry")
 group.appendChild(e)

 # "Description","Id","PIN","Email","Notes"
 # to
 # <title></title>
 # <username></username>
 # <password></password>
 # <comment></comment>

 etitle = doc.createElement("title")
 e.appendChild(etitle)
 etitle.appendChild(doc.createTextNode(row[0]))

 euser = doc.createElement("username")
 e.appendChild(euser)
 euser.appendChild(doc.createTextNode(row[1]))

 epass = doc.createElement("password")
 e.appendChild(epass)
 epass.appendChild(doc.createTextNode(row[2]))

 ecomment = doc.createElement("comment")
 e.appendChild(ecomment)
 ecomment.appendChild(doc.createTextNode(row[4]))
 

xml_out = open("converted.xml", "w")
s = doc.toxml()
xml_out.write(s)
xml_out.close()

Leave a Reply

*