Skip to content

Instantly share code, notes, and snippets.

@arudmin
Last active November 14, 2019 19:01
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save arudmin/52aec759a8c5d7543e36 to your computer and use it in GitHub Desktop.
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)
#!/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()
@arudmin
Copy link
Author

arudmin commented Jun 1, 2014

Using script

  1. Export data from KeepassX 0.4.3 using the File > Export To > Keepass XML File
  2. Save xml file with name keepassx.xml into directory with this script file
  3. Run this script with command: pythton ./keepassx-to-1passwords.py (if OK you will see generated file 1password.csv in the same directory)
  4. Open 1Password Application and select File > Import
  5. Choose File Format: Comma Delimited Text and select file 1password.csv then click Open
  6. When done, you will see imported data from KeepassX Program
Finish!

@Micronarrativ
Copy link

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.

@gasi
Copy link

gasi commented Sep 29, 2014

I added support for multiline comments here: https://github.com/gasi/keepass-to-1password

@ansgarm
Copy link

ansgarm commented Dec 27, 2015

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.

@kstenschke
Copy link

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

@mathijsbrand
Copy link

mathijsbrand commented Jun 7, 2016

Safe yourself some time and use the official supported converter. It doesn't have the issues this one does:
https://support.1password.com/import/

@BrBill
Copy link

BrBill commented Nov 14, 2019

@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