references/exports.md
3.9 KB查看排版效果| 1 | # Export Formats |
| 2 | |
| 3 | These examples target Pyzotero 1.15.2. Set the format and its parameters together on the read call. Calling `add_parameters(format='bibtex')` and then `top(limit=50)` resets the format to JSON in this release; the result will not have `.entries`. |
| 4 | |
| 5 | ## BibTeX |
| 6 | |
| 7 | ```python |
| 8 | import bibtexparser |
| 9 | |
| 10 | bibtex_db = zot.top(format='bibtex', limit=50) |
| 11 | # bibtexparser 1.x BibDatabase |
| 12 | for entry in bibtex_db.entries: |
| 13 | print(entry.get('title'), entry.get('author')) |
| 14 | with open('library.bib', 'w', encoding='utf-8') as f: |
| 15 | bibtexparser.dump(bibtex_db, f) |
| 16 | ``` |
| 17 | |
| 18 | Use `zot.everything(zot.top(format='bibtex', limit=100))` for a complete paginated remote export. The bibliography reflects Zotero's stored metadata; confirm DOI, author, title, and publication details before citing. |
| 19 | |
| 20 | ## CSL-JSON |
| 21 | |
| 22 | ```python |
| 23 | # Direct CSL export wraps the array in an 'items' member. |
| 24 | csl_export = zot.items(format='csljson', limit=50) |
| 25 | csl_items = csl_export['items'] |
| 26 | |
| 27 | # Or retain Zotero metadata alongside per-item CSL data |
| 28 | records = zot.items(format='json', include='data,csljson', limit=50) |
| 29 | csl_items = [record['csljson'] for record in records] |
| 30 | ``` |
| 31 | |
| 32 | ## Bibliography HTML and Citations |
| 33 | |
| 34 | ```python |
| 35 | # Individual references embedded in JSON (also supported by the local API) |
| 36 | records = zot.items(format='json', include='data,bib', style='apa', limit=50) |
| 37 | bib_entries = [record['bib'] for record in records] |
| 38 | |
| 39 | records = zot.items(format='json', include='citation', style='apa', limit=50) |
| 40 | citations = [record['citation'] for record in records] |
| 41 | ``` |
| 42 | |
| 43 | For one complete, style-sorted bibliography, use `format='bib'`. It has no sorting or pagination parameters and is limited to 150 matching items. In 1.15.2 its HTML response is returned as **bytes**, not a list of citation strings: |
| 44 | |
| 45 | ```python |
| 46 | html_bytes = zot.collection_items('COLKEY', format='bib', style='apa') |
| 47 | html = html_bytes.decode('utf-8') |
| 48 | ``` |
| 49 | |
| 50 | Pass a valid style identifier from the [Zotero style repository](https://www.zotero.org/styles), such as `apa`, `vancouver`, `ieee`, or `nature`; avoid assuming a renamed style alias still exists. `linkwrap='1'` wraps bibliography URLs in links. |
| 51 | |
| 52 | ## RIS and Other Exports |
| 53 | |
| 54 | The [API format table](https://www.zotero.org/support/dev/web_api/v3/basics#export_formats) lists `ris`, `biblatex`, `rdf_dc`, `rdf_zotero`, `wikipedia`, and others. Pyzotero 1.15.2 dispatches by response content type. A live direct `format='ris'` response used `application/x-research-info-systems`, which the SDK tried to parse as JSON and failed. Use per-item JSON `include` instead: |
| 55 | |
| 56 | ```python |
| 57 | from pathlib import Path |
| 58 | |
| 59 | records = zot.everything(zot.items(format='json', include='ris', limit=50)) |
| 60 | Path('library.ris').write_text('\n'.join(r['ris'] for r in records), encoding='utf-8') |
| 61 | ``` |
| 62 | |
| 63 | JSON records work with `everything()`. Direct exports may return a dict, bytes, or a BibDatabase depending on their content type; do not pass them all through one generic writer. Direct `biblatex` uses the BibTeX parser in this release. Export formats require a limit (except `bib`). |
| 64 | |
| 65 | Legacy `content='ris'`, `content='csljson'`, `content='bib'`, or `content='citation'` forces Atom and returns parsed lists in remote mode. If used, pass `content`, `style`, and `limit` on the **same** call. The local API does not support Atom. Prefer JSON `include` for per-item export data; unlike the SDK's legacy `content` parser, it supports multiple comma-separated members such as `include='data,bib'`. |
| 66 | |
| 67 | ## Keys and Versions |
| 68 | |
| 69 | ```python |
| 70 | # Omit the SDK limit for these formats, which the remote API does not cap. |
| 71 | keys_bytes = zot.items(format='keys', limit=None) |
| 72 | keys = keys_bytes.decode('utf-8').splitlines() |
| 73 | |
| 74 | # Returns a dict, not a list; do not wrap it in everything(). |
| 75 | versions = zot.item_versions() # equivalent to items(format='versions', limit=None) |
| 76 | ``` |
| 77 | |
| 78 | Source: [API response formats](https://www.zotero.org/support/dev/web_api/v3/basics#request_parameters), [release response dispatch](https://github.com/urschrei/pyzotero/blob/v1.15.2/src/pyzotero/_decorators.py). |
文件内容是平台检查时读出的文本,只供查看。平台不会运行包里的任何脚本或程序。

