Skip to content

Instantly share code, notes, and snippets.

@Techbrunch
Created November 18, 2014 22:35
Show Gist options
  • Save Techbrunch/96607a107f2857a83f56 to your computer and use it in GitHub Desktop.
Save Techbrunch/96607a107f2857a83f56 to your computer and use it in GitHub Desktop.
Small script to convert Keepass XML file to CVS that can be imported by 1Password
require 'nokogiri'
require 'base64'
require 'csv'
file = File.new("1password.csv", "w")
f = File.open(ARGV[0])
doc = Nokogiri::XML(f)
f.close
items = doc.xpath('//Group/Entry')
puts items.count
CSV.open("1password.csv", "w") do |csv|
csv.puts(['title','url','username','password','comment'])
items.each do |item|
title = item.xpath('String[Key="Title"]/Value').text.force_encoding('UTF-8').empty? ? 'No title' : item.xpath('String[Key="Title"]/Value').text.force_encoding('UTF-8')
username = item.xpath('String[Key="UserName"]/Value').text.force_encoding('UTF-8')
password = item.xpath('String[Key="Password"]/Value').text.force_encoding('UTF-8')
url = item.xpath('String[Key="URL"]/Value').text.force_encoding('UTF-8')
comment = item.xpath('String[Key="Notes"]/Value').text.force_encoding('UTF-8')
# https://learn2.agilebits.com/1Password4/Mac/en/KB/import.html
csv.puts([title,url,username,password,comment])
end
end
file.close
@Techbrunch
Copy link
Author

Usage: ./keepass_to_1password.rb keepass.xml

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