Pyzotero
Pyzotero 是一个用于 Zotero API v3 的 Python 封装库。使用它来编程管理 Zotero 库:读取条目和集合、创建和更新参考文献、上传附件、管理标签以及导出引文。
认证设置
所需凭证 — 从 https://www.zotero.org/settings/keys 获取:
- 用户 ID:显示为 "Your userID for use in API calls"
- API 密钥:在 https://www.zotero.org/settings/keys/new 创建
- 库 ID:对于群组库,为群组 URL 中
/groups/后面的数字
将凭证存储在环境变量或 .env 文件中:
ZOTERO_LIBRARY_ID=your_user_id
ZOTERO_API_KEY=your_api_key
ZOTERO_LIBRARY_TYPE=user # 或 "group"
查看 references/authentication.md 获取完整设置细节。
安装
uv add pyzotero
# 或带 CLI 支持:
uv add "pyzotero[cli]"
快速开始
from pyzotero import Zotero
zot = Zotero(library_id='123456', library_type='user', api_key='ABC1234XYZ')
# 检索顶级条目(默认返回 100 条)
items = zot.top(limit=10)
for item in items:
print(item['data']['title'], item['data']['itemType'])
# 按关键词搜索
results = zot.items(q='machine learning', limit=20)
# 检索所有条目(使用 everything() 获取完整结果)
all_items = zot.everything(zot.items())
核心概念
- 一个
Zotero实例绑定到一个库(用户或群组)。所有方法都对该库进行操作。 - 条目数据位于
item['data']中。访问字段如item['data']['title']、item['data']['creators']。 - Pyzotero 默认返回 100 个条目(API 默认值为 25)。使用
zot.everything(zot.items())获取所有条目。 - 写入方法成功时返回
True,失败时引发ZoteroError。
参考文件
| 文件 | 内容 |
|---|---|
| references/authentication.md | 凭证、库类型、本地模式 |
| references/read-api.md | 检索条目、集合、标签、群组 |
| references/search-params.md | 过滤、排序、搜索参数 |
| references/write-api.md | 创建、更新、删除条目 |
| references/collections.md | 集合的增删改查操作 |
| references/tags.md | 标签检索和管理 |
| references/files-attachments.md | 文件检索和附件上传 |
| references/exports.md | BibTeX、CSL-JSON、参考文献导出 |
| references/pagination.md | follow()、everything()、生成器 |
| references/full-text.md | 全文内容索引和检索 |
| references/saved-searches.md | 已保存搜索管理 |
| references/cli.md | 命令行界面使用 |
| references/error-handling.md | 错误和异常处理 |
常见模式
获取并修改条目
item = zot.item('ITEMKEY')
item['data']['title'] = 'New Title'
zot.update_item(item)
从模板创建条目
template = zot.item_template('journalArticle')
template['title'] = 'My Paper'
template['creators'][0] = {'creatorType': 'author', 'firstName': 'Jane', 'lastName': 'Doe'}
zot.create_items([template])
导出为 BibTeX
zot.add_parameters(format='bibtex')
bibtex = zot.top(limit=50)
# bibtex 是一个 bibtexparser BibDatabase 对象
print(bibtex.entries)
本地模式(只读,无需 API 密钥)
zot = Zotero(library_id='123456', library_type='user', local=True)
items = zot.items()




