Python library to interact with keepass databases (supports KDBX3 and KDBX4)
—
Group (folder) operations for organizing database structure including hierarchical management, creation, deletion, and search functionality.
Create new groups (folders) to organize entries hierarchically within the database.
def add_group(destination_group, group_name, icon=None, notes=None):
"""
Create a new group in the specified parent group.
Parameters:
- destination_group (Group): Parent group where new group will be created
- group_name (str): Name for the new group
- icon (str, optional): Icon ID for the group (use icons from pykeepass.icons)
- notes (str, optional): Notes for the group
Returns:
Group: The newly created group object
"""Usage Examples:
from pykeepass import PyKeePass
from pykeepass.icons import FOLDER_OPEN, LOCK_CLOSED
kp = PyKeePass('database.kdbx', password='secret')
root = kp.root_group
# Basic group creation
social_group = kp.add_group(root, 'Social Media')
# Group with icon and notes
work_group = kp.add_group(
root,
'Work Accounts',
icon=LOCK_CLOSED,
notes='All work-related passwords and accounts'
)
# Nested group creation
dev_group = kp.add_group(work_group, 'Development Tools')
kp.save()Comprehensive search functionality for finding groups by various criteria with regex support.
def find_groups(**kwargs):
"""
Find groups with flexible criteria.
Parameters:
- name (str, optional): Match group name
- path (str, optional): Match full path
- uuid (str, optional): Match UUID
- notes (str, optional): Match notes field
- group (Group, optional): Limit search to specific parent group
- regex (bool): Use regex matching
- flags (int, optional): Regex flags
- first (bool): Return only first match
Returns:
list or Group: List of matching groups, or single group if first=True
"""
def find_groups_by_name(group_name, regex=False, flags=None, group=None, first=False):
"""
Find groups by name.
Parameters:
- group_name (str): Group name to search for
- regex (bool): Use regex matching
- flags (int, optional): Regex flags
- group (Group, optional): Limit search to specific parent group
- first (bool): Return only first match
Returns:
list or Group: Matching groups
"""
def find_groups_by_path(group_path_str=None, regex=False, flags=None, group=None, first=False):
"""
Find groups by full path.
Parameters:
- group_path_str (str): Full group path to search for
- regex (bool): Use regex matching
- flags (int, optional): Regex flags
- group (Group, optional): Limit search to specific parent group
- first (bool): Return only first match
Returns:
list or Group: Matching groups
"""
def find_groups_by_uuid(uuid, regex=False, flags=None, group=None, history=False, first=False):
"""Find groups by UUID."""
def find_groups_by_notes(notes, regex=False, flags=None, group=None, history=False, first=False):
"""Find groups by notes field."""Usage Examples:
import re
# Basic searches
social_groups = kp.find_groups(name='Social')
work_group = kp.find_groups_by_name('Work', first=True)
# Path-based search
dev_group = kp.find_groups_by_path('Root/Work/Development', first=True)
# Regex searches
media_groups = kp.find_groups_by_name(r'.*[Mm]edia.*', regex=True)
important_groups = kp.find_groups_by_notes('important', regex=True, flags=re.IGNORECASE)
# Limit search scope
work_group = kp.find_groups_by_name('Work', first=True)
sub_groups = kp.find_groups(group=work_group)
# Find all groups
all_groups = kp.groupsModify existing groups and manage their lifecycle including deletion and movement.
def delete_group(group):
"""
Delete a group and all its contents from the database.
Parameters:
- group (Group): Group object to delete
Note: This will also delete all entries and subgroups within the group
"""
def move_group(group, destination_group):
"""
Move a group to a different parent group.
Parameters:
- group (Group): Group to move
- destination_group (Group): New parent group
"""Usage Examples:
# Delete empty group
old_group = kp.find_groups_by_name('Temporary', first=True)
if old_group and not old_group.entries and not old_group.subgroups:
kp.delete_group(old_group)
# Move group to new parent
social_group = kp.find_groups_by_name('Social Media', first=True)
personal_group = kp.find_groups_by_name('Personal', first=True)
kp.move_group(social_group, personal_group)
kp.save()The Group class represents hierarchical containers (folders) for organizing entries and other groups.
class Group:
def __init__(name=None, element=None, icon=None, notes=None, kp=None, expires=None, expiry_time=None):
"""Create a new Group object."""
@property
def name() -> str:
"""Group name"""
@property
def notes() -> str:
"""Group notes"""
@property
def entries() -> list:
"""Entries in this group as list of Entry objects"""
@property
def subgroups() -> list:
"""Child groups as list of Group objects"""
@property
def is_root_group() -> bool:
"""Whether this is the root group"""
@property
def path() -> str:
"""Full path to group"""
def append(entries):
"""
Add entries or groups to this group.
Parameters:
- entries (Entry or Group or list): Entry/Group objects or list of objects to add
"""Usage Examples:
# Access group properties
work_group = kp.find_groups_by_name('Work', first=True)
print(f"Group name: {work_group.name}")
print(f"Group path: {work_group.path}")
print(f"Notes: {work_group.notes}")
# Check if root group
if work_group.is_root_group:
print("This is the root group")
# Access group contents
print(f"Entries in group: {len(work_group.entries)}")
print(f"Subgroups: {len(work_group.subgroups)}")
# List all entries in group
for entry in work_group.entries:
print(f" Entry: {entry.title}")
# List all subgroups
for subgroup in work_group.subgroups:
print(f" Subgroup: {subgroup.name}")Add entries and groups to existing groups programmatically.
def append(entries):
"""
Add entries or groups to this group.
Parameters:
- entries (Entry or Group or list): Entry/Group objects or list of objects to add to this group
"""Usage Examples:
# Move existing entry to group
entry = kp.find_entries_by_title('Facebook', first=True)
social_group = kp.find_groups_by_name('Social', first=True)
social_group.append(entry)
# Move multiple entries
entries = kp.find_entries_by_string('work', regex=True)
work_group = kp.find_groups_by_name('Work', first=True)
work_group.append(entries) # Can pass a list
# Move subgroup
dev_group = kp.find_groups_by_name('Development', first=True)
tech_group = kp.find_groups_by_name('Technology', first=True)
tech_group.append(dev_group)
kp.save()Navigate and work with the hierarchical group structure.
Usage Examples:
# Start from root and navigate down
root = kp.root_group
print(f"Root group: {root.name}")
# Find specific paths
for subgroup in root.subgroups:
print(f"Top-level group: {subgroup.name}")
for sub_subgroup in subgroup.subgroups:
print(f" Subgroup: {sub_subgroup.name}")
# Get full hierarchy paths
def print_group_tree(group, indent=0):
print(" " * indent + f"Group: {group.name}")
for entry in group.entries:
print(" " * (indent + 1) + f"Entry: {entry.title}")
for subgroup in group.subgroups:
print_group_tree(subgroup, indent + 1)
print_group_tree(root)
# Navigate by path components
path_parts = ['Work', 'Development', 'GitHub']
current_group = root
for part in path_parts:
current_group = next(
(g for g in current_group.subgroups if g.name == part),
None
)
if not current_group:
break
if current_group:
print(f"Found group: {current_group.path}")Common patterns for organizing groups effectively.
Usage Examples:
# Create organized structure
root = kp.root_group
# By category
personal = kp.add_group(root, 'Personal')
work = kp.add_group(root, 'Work')
finance = kp.add_group(root, 'Finance')
# By access level
kp.add_group(work, 'Public')
kp.add_group(work, 'Internal')
kp.add_group(work, 'Confidential')
# By platform/service type
kp.add_group(personal, 'Social Media')
kp.add_group(personal, 'Email Accounts')
kp.add_group(personal, 'Shopping')
kp.add_group(work, 'Development Tools')
kp.add_group(work, 'Cloud Services')
# By project
projects = kp.add_group(work, 'Projects')
kp.add_group(projects, 'Project Alpha')
kp.add_group(projects, 'Project Beta')
kp.save()Install with Tessl CLI
npx tessl i tessl/pypi-pykeepass