Guest User

Untitled

a guest
Jan 21st, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # Convert 1Password .csv file to the KeePassX xml format
  4. # Note: Ignores categories
  5. import sys
  6. import csv
  7. import unicodedata
  8. import lxml.etree as ET
  9.  
  10. # Maps fields names from 1Password to KeepPassX
  11. FIELD_MAP = {
  12. 'title': 'title',
  13. 'username': 'username',
  14. 'password': 'password',
  15. 'URL/Location': 'url',
  16. 'notes': 'comment'
  17. }
  18.  
  19. if len(sys.argv) != 3:
  20. print 'Usage: ./convert <input 1Password file> <output keepassx file>'
  21. sys.exit(1)
  22.  
  23. input_file = sys.argv[1]
  24. output_file = sys.argv[2]
  25.  
  26. with open(input_file, 'r') as fp:
  27. reader = csv.DictReader(fp, delimiter='\t',
  28. doublequote=True,
  29. quoting=csv.QUOTE_ALL)
  30.  
  31. root = ET.Element('database')
  32. group = ET.SubElement(root, 'group')
  33. title = ET.SubElement(group, 'title')
  34. title.text = 'Internet'
  35. icon = ET.SubElement(group, 'icon')
  36. icon.text = '1'
  37.  
  38. skipped = 0
  39. for index, row in enumerate(reader):
  40. entry = ET.SubElement(group, 'entry')
  41.  
  42. for name1, name2 in FIELD_MAP.items():
  43. value = unicode(row[name1], 'utf-8')
  44.  
  45. if name1 == 'notes' and value == '\\n\\n':
  46. value = u''
  47.  
  48. unicodedata.normalize('NFKD', value).encode('ascii', 'ignore')
  49. item = ET.SubElement(entry, name2)
  50. item.text = value
  51.  
  52. print 'Processed %d entries' % (index - skipped)
  53.  
  54. tree = ET.ElementTree(root)
  55. tree.write(output_file)
  56.  
  57. print 'Written to %s' % (output_file)
Add Comment
Please, Sign In to add comment