Search code examples
pythonkeepass

How to create kdbx file in python


I'd like to know if there is a way, in python to create a keypass file (.kdbx). With this, I want to fill it with passwords and export it. I tried to juste create a file and use the pykeepass module to fill it but I met errors :

f = open("test.kdbx", "w")
kp = PyKeePass("test.kdbx", "test")
group = kp.add_group(kp.root_group, "test")
kp.add_entry(group, "coucou", "username", "password")
kp.save()
f.close()

but it threw me : enter image description here

(the file is empty)

Do you know a way to fill this file correctly ?


Solution

  • Follow the example in PyKeepass:

    Instead of kp = PyKeePass('db.kdbx', password='somePassw0rd') to load an existing database, use create_database() to create a new one:

    from pykeepass import PyKeePass, create_database
    
    # create a new database at filename with supplied credentials.
    # returns PyKeePass object.
    kp = create_database ('newDb.kdbx', password='s3cr3t', keyfile=None, transformed_key=None)
    
    # create a new group
    group = kp.add_group(kp.root_group, 'email')
    
    # create a new entry
    entry = kp.add_entry(group, 'gmail', 'myusername', 'myPassw0rdXX')
    print(entry)  # Entry: "email/gmail (myusername)"
    
    # save database
    kp.save()