pykeepass

libkeepassGPL-3.0活跃维护

用 Python 读写 KeePass 密码库的库,支持 KDBX3 和 KDBX4 格式,方便在脚本里自动化管理密码条目。

Python library to interact with keepass databases (supports KDBX3 and KDBX4)

v4.2.0发布于 2026-08-09 21:22 UTC510star104fork同步于 2026-11-07 07:33 UTC

怎么上手

README 里没有找到可直接复制的安装命令

README 里没有找到可直接复制的安装命令,请到 README 或官方文档查看安装方式。

硬件要求

README 没有说明硬件要求。

下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

这个项目的说明还在生成中,先参考下面「最近发版」和 README。

最近发版

5 个版本v4.2.0
  1. v4.2.0最新正式版2026-08-09 · 2026-08-09 21:22 UTC
    • fixed #219 - seeds not regenerated on save
  2. v4.1.1.post12025-03-06 · 2025-03-06 00:41 UTC
    查看更新说明
    • fixed #410 - support empty string as password
  3. v4.1.12025-03-04 · 2025-03-04 23:48 UTC
    查看更新说明
    • fixed #410 - support empty string as password
  4. v4.1.0.post12024-07-30 · 2024-07-30 18:33 UTC
    查看更新说明
    • merged PR#389 - add PyKeePass.database_name and database_description
    • merged PR#392, fixed #390 - fix pkg_resources dependency issue
    • fixed #391 - Entry.tags returns empty list instead of None
    • fixed #395 - set 'encoding' attribute when exporting as XML
    • fixed #383 - parse datetimes using isoformat instead of strptime
  5. v4.1.02024-06-26 · 2024-06-26 18:56 UTC
    查看更新说明

    4.1.0 - 2024-06-26

    • merged PR#389 - add PyKeePass.database_name and database_description
    • merged PR#392, fixed #390 - fix pkg_resources dependency issue
    • fixed #391 - Entry.tags returns empty list instead of None
    • fixed #395 - set 'encoding' attribute when exporting as XML
    • fixed #383 - parse datetimes using isoformat instead of strptime

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    pykeepass

    This library allows you to write entries to a KeePass database.

    Come chat at #pykeepass:matrix.org on Matrix.

    Installation

    sudo apt install python3-lxml
    pip install pykeepass
    

    Quickstart

    General database manipulation

    from pykeepass import PyKeePass
    
    # load database
    >>> kp = PyKeePass('db.kdbx', password='somePassw0rd')
    
    # get all entries
    >>> kp.entries
    [Entry: "foo_entry (myusername)", Entry: "foobar_entry (myusername)", ...]
    
    # find any group by its name
    >>> group = kp.find_groups(name='social', first=True)
    
    # get the entries in a group
    >>> group.entries
    [Entry: "social/facebook (myusername)", Entry: "social/twitter (myusername)"]
    
    # find any entry by its title
    >>> entry = kp.find_entries(title='facebook', first=True)
    
    # retrieve the associated password and OTP information
    >>> entry.password
    's3cure_p455w0rd'
    >>> entry.otp
    otpauth://totp/test:lkj?secret=TEST%3D%3D%3D%3D&period=30&digits=6&issuer=test
    
    # update an entry
    >>> entry.notes = 'primary facebook account'
    
    # create a new group
    >>> group = kp.add_group(kp.root_group, 'email')
    
    # create a new entry
    >>> kp.add_entry(group, 'gmail', 'myusername', 'myPassw0rdXX')
    Entry: "email/gmail (myusername)"
    
    # save database
    >>> kp.save()
    

    Finding and manipulating entries

    # add a new entry to the Root group
    >>> kp.add_entry(kp.root_group, 'testing', 'foo_user', 'passw0rd')
    Entry: "testing (foo_user)"
    
    # add a new entry to the social group
    >>> group = kp.find_groups(name='social', first=True)
    >>> entry = kp.add_entry(group, 'testing', 'foo_user', 'passw0rd')
    Entry: "testing (foo_user)"
    
    # save the database
    >>> kp.save()
    
    # delete an entry
    >>> kp.delete_entry(entry)
    
    # move an entry
    >>> kp.move_entry(entry, kp.root_group)
    
    # save the database
    >>> kp.save()
    
    # change creation time
    >>> from datetime import datetime, timezone
    >>> entry.ctime = datetime(2023, 1, 1, tzinfo=timezone.utc)
    
    # update modification or access time
    >>> entry.touch(modify=True)
    
    # save entry history
    >>> entry.save_history()
    

    Finding and manipulating groups

    >>> kp.groups
    [Group: "foo", Group "foobar", Group: "social", Group: "social/foo_subgroup"]
    
    >>> kp.find_groups(name='foo', first=True)
    Group: "foo"
    
    >>> kp.find_groups(name='foo.*', regex=True)
    [Group: "foo", Group "foobar"]
    
    >>> kp.find_groups(path=['social'], regex=True)
    [Group: "social", Group: "social/foo_subgroup"]
    
    >>> kp.find_groups(name='social', first=True).subgroups
    [Group: "social/foo_subgroup"]
    
    >>> kp.root_group
    Group: "/"
    
    # add a new group to the Root group
    >>> group = kp.add_group(kp.root_group, 'social')
    
    # add a new group to the social group
    >>> group2 = kp.add_group(group, 'gmail')
    Group: "social/gmail"
    
    # save the database
    >>> kp.save()
    
    # delete a group
    >>> kp.delete_group(group)
    
    # move a group
    >>> kp.move_group(group2, kp.root_group)
    
    # save the database
    >>> kp.save()
    
    # change creation time
    >>> from datetime import datetime, timezone
    >>> group.ctime = datetime(2023, 1, 1, tzinfo=timezone.utc)
    
    # update modification or access time
    >>> group.touch(modify=True)
    

    Attachments

    >>> e = kp.add_entry(kp.root_group, title='foo', username='', password='')
    
    # add attachment data to the db
    >>> binary_id = kp.add_binary(b'Hello world')
    
    >>> kp.binaries
    [b'Hello world']
    
    # add attachment reference to entry
    >>> a = e.add_attachment(binary_id, 'hello.txt')
    >>> a
    Attachment: 'hello.txt' -> 0
    
    # access attachments
    >>> a
    Attachment: 'hello.txt' -> 0
    >>> a.id
    0
    >>> a.filename
    'hello.txt'
    >>> a.data
    b'Hello world'
    >>> e.attachments
    [Attachment: 'hello.txt' -> 0]
    
    # list all attachments in the database
    >>> kp.attachments
    [Attachment: 'hello.txt' -> 0]
    
    # search attachments
    >>> kp.find_attachments(filename='hello.txt')
    [Attachment: 'hello.txt** -> 0]
    
    # delete attachment reference
    >>> e.delete_attachment(a)
    
    # or, delete both attachment reference and binary
    >>> kp.delete_binary(binary_id**
    

    OTP codes

    # find an entry which has otp attribute
    >>> e = kp.find_entries(otp='.*', regex=True, first=True)
    >>> import pyotp
    >>> pyotp.parse_uri(e.otp).now()
    799270
    

    Tests and Debugging

    Run tests with python tests/tests.py or python tests/tests.py SomeSpecificTest

    Enable debugging when doing tests in console:

    >>> from pykeepass.pykeepass import debug_setup
    >>> debug_setup()
    >>> kp.entries[0]
    DEBUG:pykeepass.pykeepass:xpath query: //Entry
    DEBUG:pykeepass.pykeepass:xpath query: (ancestor::Group)[last()]
    DEBUG:pykeepass.pykeepass:xpath query: (ancestor::Group)[last()]
    DEBUG:pykeepass.pykeepass:xpath query: String/Key[text()="Title"]/../Value
    DEBUG:pykeepass.pykeepass:xpath query: String/Key[text()="UserName"]/../Value
    Entry: "root_entry (foobar_user)"