Skip to content

Instantly share code, notes, and snippets.

@nicr9
Created December 12, 2016 09:29
Show Gist options
  • Save nicr9/7f714aef8fa90eb6b4a52e64060eb853 to your computer and use it in GitHub Desktop.
Save nicr9/7f714aef8fa90eb6b4a52e64060eb853 to your computer and use it in GitHub Desktop.
Lastpass to KeePassX converter
from sys import argv
from collections import defaultdict
import xml.etree.ElementTree as ET
import csv
import datetime
if __name__ == "__main__":
src = argv[1]
dest = argv[1] + ".kp.xml"
current_time = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M")
categories = defaultdict(list)
with open(src, 'r') as inp:
lpreader = csv.reader(inp, delimiter=',', quotechar='"')
header = next(lpreader)
print(header)
for row in lpreader:
categories[row[5]].append(row)
page = ET.Element('database')
doc = ET.ElementTree(page)
for category in categories:
head_element = ET.SubElement(page, "group")
ET.SubElement(head_element, "title").text = category
ET.SubElement(head_element, "icon").text = "0"
for entry in categories[category]:
entry_element = ET.SubElement(head_element, "entry")
ET.SubElement(entry_element, 'title').text = str(entry[4])
ET.SubElement(entry_element, 'username').text = str(entry[1])
ET.SubElement(entry_element, 'password').text = str(entry[2])
ET.SubElement(entry_element, 'url').text = str(entry[0])
ET.SubElement(entry_element, 'comment').text = str(entry[3])
ET.SubElement(entry_element, 'icon').text = "0"
ET.SubElement(entry_element, 'creation').text = current_time
ET.SubElement(entry_element, 'lastaccess').text = str(entry[5])
ET.SubElement(entry_element, 'lastmod').text = current_time
ET.SubElement(entry_element, 'expire').text = "Never"
with open(dest, 'wb') as outp:
outp.write("<!DOCTYPE KEEPASSX_DATABASE>".encode("utf-8"))
doc.write(outp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment