Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pieterbreed/cccc5ba889581f0740bea398c6eae5f8 to your computer and use it in GitHub Desktop.
Save pieterbreed/cccc5ba889581f0740bea398c6eae5f8 to your computer and use it in GitHub Desktop.
Convert exported XML file from KeepassX that could be imported to 1Password (via CSV comma separated file)
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import re
from lxml import etree
import StringIO
import sys
import string
reload(sys)
sys.setdefaultencoding('utf-8')
file = open('1password.csv','w')
parser = etree.XMLParser(encoding='utf-8', recover=True)
tree = etree.parse('keepassx.xml', parser)
groups = tree.xpath('/database/group')
# for group in range(len(groups)):
# subgroup = groups[group].xpath('group')
# if (len(subgroup)):
# # print len(subgroup)
# for node in subgroup:
# print len(subgroup), groups[group].getchildren()[0].text, node[0].text
# else:
# print len(subgroup), groups[group].getchildren()[0].text, len(groups[group].xpath('entry'))
def getnodes(gr, num):
subgroup = gr.xpath('group')
entry = gr.xpath('entry')
groupTitle = gr[0].text
if (len(entry)):
for item in entry:
title = string.replace(str(item[0].text), "\"", "\"\"")
username = string.replace(str(item[1].text), "\"", "\"\"")
password = string.replace(str(item[2].text), "\"", "\"\"")
url = string.replace(str(item[3].text), "\"", "\"\"")
comment = string.replace(str(item[4].text), "\"", "\"\"")
# print 'Title:', item[0].text.encode("UTF-8")
s = ('"'+title+'","'+url+'","'+username+'","'+password+'","'+comment+'"').replace('None','')
file.write(s+'\n')
if(len(subgroup)):
for node in range(len(subgroup)):
# print 'getnodes'
getnodes(subgroup[node], node)
for group in range(len(groups)):
getnodes(groups[group], group)
file.close()
@pieterbreed
Copy link
Author

Added code to escape any " that might be present in passwords, usernames or notes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment