Skip to content

Instantly share code, notes, and snippets.

@PaulGoldschmidt
Last active October 19, 2024 15:54
Show Gist options
  • Save PaulGoldschmidt/408725fb4c328dfda3b7ba124dc688ab to your computer and use it in GitHub Desktop.
Save PaulGoldschmidt/408725fb4c328dfda3b7ba124dc688ab to your computer and use it in GitHub Desktop.
A script to transform the keepass .csv-export to a format that is importable by the native apple password application.
import csv
import getpass
from pykeepass import PyKeePass
# Function to decrypt the KeeWeb file and transform it to CSV
def transform_kdbx_to_csv(kdbx_file, output_csv):
# Ask for KeeWeb password securely via CLI
password = getpass.getpass("Enter KeeWeb (KeePass) database password: ")
# Open the KeePass database using pykeepass
try:
kp = PyKeePass(kdbx_file, password=password)
except Exception as e:
print(f"Failed to open KeePass database: {e}")
return
# Prepare to write the output CSV
with open(output_csv, mode='w', newline='', encoding='utf-8') as outfile:
fieldnames = ['Title', 'URL', 'Username', 'Password', 'Notes', 'OTPAuth']
writer = csv.DictWriter(outfile, fieldnames=fieldnames)
writer.writeheader()
# Loop through all entries in the KeePass database
for entry in kp.entries:
transformed_row = {
'Title': entry.title,
'URL': entry.url if entry.url else '',
'Username': entry.username if entry.username else '',
'Password': entry.password if entry.password else '',
'Notes': entry.notes if entry.notes else '',
'OTPAuth': '' # Leaving OTPAuth blank
}
writer.writerow(transformed_row)
print(f"CSV file saved as: {output_csv}")
# Main function to run the transformation
if __name__ == "__main__":
kdbx_file = 'database.kdbx' # Path to your KeeWeb (.kdbx) file
output_csv = 'passwords.csv' # Path for the output CSV
# Transform KeeWeb encrypted file to CSV
transform_kdbx_to_csv(kdbx_file, output_csv)
import csv
# Input and output CSV file paths
input_file = 'keydb.csv'
output_file = 'passwords.csv'
# Function to transform the input CSV format to the desired format
def transform_csv(input_file, output_file):
# Open the input CSV file for reading
with open(input_file, mode='r', newline='', encoding='utf-8') as infile:
reader = csv.DictReader(infile)
# Prepare the fieldnames for the output CSV
fieldnames = ['Title', 'URL', 'Username', 'Password', 'Notes', 'OTPAuth']
# Open the output CSV file for writing
with open(output_file, mode='w', newline='', encoding='utf-8') as outfile:
writer = csv.DictWriter(outfile, fieldnames=fieldnames)
# Write the header row
writer.writeheader()
# Transform and write each row from input to output
for row in reader:
transformed_row = {
'Title': row['Account'], # Account becomes Title
'URL': row['Web Site'], # Web Site becomes URL
'Username': row['Login Name'], # Login Name becomes Username
'Password': row['Password'], # Password remains the same
'Notes': row['Comments'], # Comments become Notes
'OTPAuth': '' # OTPAuth remains blank
}
writer.writerow(transformed_row)
# Run the transformation
transform_csv(input_file, output_file)
import xml.etree.ElementTree as ET
import csv
# Function to parse the XML and convert to CSV
def xml_to_csv(xml_file, output_csv):
# Parse the XML file
tree = ET.parse(xml_file)
root = tree.getroot()
# Open the CSV file for writing
with open(output_csv, mode='w', newline='', encoding='utf-8') as outfile:
fieldnames = ['Title', 'Username', 'Password', 'URL', 'Notes', 'OTPAuth']
writer = csv.DictWriter(outfile, fieldnames=fieldnames)
writer.writeheader()
# Find all entries in the XML
for entry in root.findall('.//Entry'):
title = ''
username = ''
password = ''
url = ''
notes = ''
# Iterate through all <String> elements inside <Entry>
for string in entry.findall('String'):
key = string.find('Key').text
value = string.find('Value').text if string.find('Value') is not None else ''
if key == 'Title':
title = value
elif key == 'UserName':
username = value
elif key == 'Password':
password = value
elif key == 'URL':
url = value
elif key == 'Notes':
notes = value
# Write to CSV row
writer.writerow({
'Title': title,
'Username': username,
'Password': password,
'URL': url,
'Notes': notes,
'OTPAuth': '' # Leave OTPAuth blank
})
print(f"CSV file saved as: {output_csv}")
# Main function to run the transformation
if __name__ == "__main__":
xml_file = 'database.xml' # Replace with the path to your XML file
output_csv = 'output.csv' # Output CSV file
# Transform the XML to CSV
xml_to_csv(xml_file, output_csv)
@PaulGoldschmidt
Copy link
Author

For the direct transformer to work, install pykeepass with pip install pykeepass. Python 3.2 or higher recommended.

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