Last active
August 30, 2024 17:03
-
-
Save arudmin/52aec759a8c5d7543e36 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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
import os | |
import re | |
from lxml import etree | |
import StringIO | |
import sys | |
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 = str(item[0].text) | |
username = str(item[1].text) | |
password = str(item[2].text) | |
url = str(item[3].text) | |
comment = 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() |
I added support for multiline comments here: https://github.com/gasi/keepass-to-1password
PSA: If you have Double Quotes "
in your passwords 1Password 5 silently fails (for me at least). You have to place another "
to escape the one in your password.
Hi arudmin, thankyou for this script 👍
I first received an error: "ImportError: No module named lxml", this was fixed by (maybe you want to add a hint to step 3) running: pip install --upgrade lxml
Safe yourself some time and use the official supported converter. It doesn't have the issues this one does:
https://support.1password.com/import/
@mathijsbrand, That official conversion method doesn't support Keepass unless it's exported to CSV.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The script does not preserve linebreaks a la
in the comment field. When converting the xml file to csv file all comments are truncated after the first line. I am useless at Python, so you might want to add this your self.