Skills › Scientific Agent Skills › pyzotero › collections.md
collections.md
131 lines · skills/pyzotero/references/collections.md at 5f2bc03
Collection Management
Raw lines 1 to 23## Reading Collections
5```python
6# All collections (flat list including nested)
7all_cols = zot.everything(zot.collections())
9# Only top-level collections
10top_cols = zot.collections_top()
12# Specific collection
13col = zot.collection('COLKEY')
15# Sub-collections of a collection
16sub_cols = zot.collections_sub('COLKEY')
18# All collections under a given collection (recursive)
19tree = zot.all_collections('COLKEY')
20# Or all collections in the library:
21tree = zot.all_collections()
22```
Reading Collections
# All collections (flat list including nested)
all_cols = zot.everything(zot.collections())
# Only top-level collections
top_cols = zot.collections_top()
# Specific collection
col = zot.collection('COLKEY')
# Sub-collections of a collection
sub_cols = zot.collections_sub('COLKEY')
# All collections under a given collection (recursive)
tree = zot.all_collections('COLKEY')
# Or all collections in the library:
tree = zot.all_collections()Raw lines 3 to 2324## Collection Data Structure
26```python
27col = zot.collection('5TSDXJG6')
28name = col['data']['name']
29key = col['data']['key']
30parent = col['data']['parentCollection'] # False if top-level, else parent key
31version = col['data']['version']
32n_items = col['meta']['numItems']
33n_sub_collections = col['meta']['numCollections']
34```
Collection Data Structure
col = zot.collection('5TSDXJG6')
name = col['data']['name']
key = col['data']['key']
parent = col['data']['parentCollection'] # False if top-level, else parent key
version = col['data']['version']
n_items = col['meta']['numItems']
n_sub_collections = col['meta']['numCollections']Raw lines 24 to 3536## Creating Collections
38```python
39# Create a top-level collection
40zot.create_collections([{'name': 'My New Collection'}])
42# Create a nested collection
43zot.create_collections([{
44 'name': 'Sub-Collection',
45 'parentCollection': 'PARENTCOLKEY'
46}])
48# Create a parent, inspect the result, then use its real key for a child.
49result = zot.create_collections([{'name': 'Collection B'}])
50if result.get('failed'):
51 raise RuntimeError(result['failed'])
52parent_key = result['successful']['0']['key']
53child_result = zot.create_collections([{'name': 'Sub-B', 'parentCollection': parent_key}])
54if child_result.get('failed'):
55 raise RuntimeError(child_result['failed'])
56```
Creating Collections
# Create a top-level collection
zot.create_collections([{'name': 'My New Collection'}])
# Create a nested collection
zot.create_collections([{
'name': 'Sub-Collection',
'parentCollection': 'PARENTCOLKEY'
}])
# Create a parent, inspect the result, then use its real key for a child.
result = zot.create_collections([{'name': 'Collection B'}])
if result.get('failed'):
raise RuntimeError(result['failed'])
parent_key = result['successful']['0']['key']
child_result = zot.create_collections([{'name': 'Sub-B', 'parentCollection': parent_key}])
if child_result.get('failed'):
raise RuntimeError(child_result['failed'])Raw lines 36 to 5758## Updating Collections
60```python
61cols = zot.collections()
62# Rename the first collection, if there is one
63if cols:
64 cols[0]['data']['name'] = 'Renamed Collection'
65 zot.update_collection(cols[0])
67# For multiple collections, use individually versioned updates.
68# Re-fetch after a prior write to avoid reusing the old version.
69for col in cols:
70 fresh = zot.collection(col['key'])
71 fresh['data']['name'] = col['data']['name']
72 zot.update_collection(fresh)
73```
Updating Collections
cols = zot.collections()
# Rename the first collection, if there is one
if cols:
cols[0]['data']['name'] = 'Renamed Collection'
zot.update_collection(cols[0])
# For multiple collections, use individually versioned updates.
# Re-fetch after a prior write to avoid reusing the old version.
for col in cols:
fresh = zot.collection(col['key'])
fresh['data']['name'] = col['data']['name']
zot.update_collection(fresh)Raw lines 58 to 7475## Deleting Collections
77```python
78# Delete a single collection
79col = zot.collection('COLKEY')
80zot.delete_collection(col)
82# Delete multiple collections
83cols = zot.collections(limit=50) # review the selected keys; max 50
84library_version = int(zot.request.headers['Last-Modified-Version'])
85if cols:
86 zot.delete_collection(cols, last_modified=library_version)
87```
Deleting Collections
# Delete a single collection
col = zot.collection('COLKEY')
zot.delete_collection(col)
# Delete multiple collections
cols = zot.collections(limit=50) # review the selected keys; max 50
library_version = int(zot.request.headers['Last-Modified-Version'])
if cols:
zot.delete_collection(cols, last_modified=library_version)Raw lines 75 to 8889## Managing Items in Collections
91```python
92# Add an item to a collection
93item = zot.item('ITEMKEY')
94zot.addto_collection('COLKEY', item)
96# Remove an item from a collection (re-fetch after any earlier write)
97item = zot.item('ITEMKEY')
98zot.deletefrom_collection('COLKEY', item)
100# Move between collections with one version-checked request
101item = zot.item('ITEMKEY')
102zot.moveto_collection('OLDCOLKEY', 'NEWCOLKEY', item)
104# Get all items in a collection
105items = zot.everything(zot.collection_items('COLKEY'))
107# Get only top-level items in a collection
108top_items = zot.collection_items_top('COLKEY')
110# Count items in a collection
111n = zot.num_collectionitems('COLKEY')
113# Get tags in a collection
114tags = zot.collection_tags('COLKEY')
115```
Managing Items in Collections
# Add an item to a collection
item = zot.item('ITEMKEY')
zot.addto_collection('COLKEY', item)
# Remove an item from a collection (re-fetch after any earlier write)
item = zot.item('ITEMKEY')
zot.deletefrom_collection('COLKEY', item)
# Move between collections with one version-checked request
item = zot.item('ITEMKEY')
zot.moveto_collection('OLDCOLKEY', 'NEWCOLKEY', item)
# Get all items in a collection
items = zot.everything(zot.collection_items('COLKEY'))
# Get only top-level items in a collection
top_items = zot.collection_items_top('COLKEY')
# Count items in a collection
n = zot.num_collectionitems('COLKEY')
# Get tags in a collection
tags = zot.collection_tags('COLKEY')Raw lines 89 to 116117## Find Collection Key by Name
119```python
120def find_collection(zot, name):
121 for col in zot.everything(zot.collections()):
122 if col['data']['name'] == name:
123 return col['data']['key']
124 return None
126key = find_collection(zot, 'Machine Learning Papers')
127```
129Collection deletion also removes descendant collections, but preserves their items in the library. Create and batch-update responses use the same `successful`/`success`/`unchanged`/`failed` maps as items; creation accepts at most 50 collections per request.
131In Pyzotero 1.15.2, `update_collections()` routes collection dictionaries through the item-field validator and can reject `name`/`parentCollection`; the per-collection `update_collection()` example avoids that path. Its single-object precondition is the collection version; multi-delete needs the library version.
Find Collection Key by Name
def find_collection(zot, name):
for col in zot.everything(zot.collections()):
if col['data']['name'] == name:
return col['data']['key']
return None
key = find_collection(zot, 'Machine Learning Papers')Collection deletion also removes descendant collections, but preserves their items in the library. Create and batch-update responses use the same successful/success/unchanged/failed maps as items; creation accepts at most 50 collections per request.
In Pyzotero 1.15.2, update_collections() routes collection dictionaries through the item-field validator and can reject name/parentCollection; the per-collection update_collection() example avoids that path. Its single-object precondition is the collection version; multi-delete needs the library version.