{"id":4183,"library":"pybtex","title":"Pybtex: BibTeX-compatible bibliography processor","description":"Pybtex is a BibTeX-compatible bibliography processor written in Python, designed to read citation information from various formats (BibTeX, BibTeXML, YAML) and produce formatted bibliographies. It supports traditional BibTeX `.bst` styles and also allows for defining custom styles directly in Python, with output options including LaTeX, HTML, Markdown, or plain text. Currently at version 0.26.1, it is actively maintained and serves as a flexible alternative or drop-in replacement for the original BibTeX.","status":"active","version":"0.26.1","language":"en","source_language":"en","source_url":"https://codeberg.org/pybtex/pybtex","tags":["bibliography","bibtex","latex","publishing","academic","markdown","html"],"install":[{"cmd":"pip install pybtex","lang":"bash","label":"Install Pybtex"}],"dependencies":[],"imports":[{"note":"Main object for holding parsed bibliography data.","symbol":"BibliographyData","correct":"from pybtex.database import BibliographyData"},{"note":"For parsing BibTeX (.bib) files.","symbol":"Parser","correct":"from pybtex.database.input.bibtex import Parser"},{"note":"For writing bibliography data to various output formats (e.g., HTML, LaTeX, text).","symbol":"Writer","correct":"from pybtex.database.output.html import Writer"},{"note":"Base class for defining custom bibliography formatting styles in Python.","symbol":"BaseStyle","correct":"from pybtex.style.formatting import BaseStyle"},{"note":"Used within Pythonic styles for rich text manipulation.","symbol":"Text","correct":"from pybtex.richtext import Text"}],"quickstart":{"code":"from pybtex.database.input import bibtex\nfrom pybtex.database.output import html\nfrom pybtex.style.formatting import plain\nfrom pybtex.style.template import field, sentence, tag\nimport os\n\n# Create a dummy .bib file for demonstration\nbib_content = \"\"\"\n@article{einstein1905relativity,\n  title={Zur Elektrodynamik bewegter K\\\"orper},\n  author={Einstein, Albert},\n  journal={Annalen der Physik},\n  volume={322},\n  number={10},\n  pages={891-921},\n  year={1905},\n  doi={10.1002/andp.19053221006}\n}\n\"\"\"\nwith open('references.bib', 'w') as f:\n    f.write(bib_content)\n\n# 1. Parse the bibliography data\nparser = bibtex.Parser()\nbib_data = parser.parse_file('references.bib')\n\n# 2. Select a formatting style (e.g., 'plain' or a custom one)\n# For a custom style, you would inherit from BaseStyle\nstyle = plain.Style()\n\n# 3. Format the bibliography\n# The 'citations' argument specifies which entries to include. \n# If not provided, all entries in bib_data will be formatted.\nformatted_bibliography = style.format_bibliography(bib_data, citations=['einstein1905relativity'])\n\n# 4. Write the formatted bibliography to an output file (e.g., HTML)\nwriter = html.Writer()\nwith open('output.html', 'w') as f:\n    writer.write_file(formatted_bibliography, f)\n\nprint(\"Bibliography processed and saved to output.html\")\n\n# Clean up dummy file\nos.remove('references.bib')\nos.remove('output.html')","lang":"python","description":"This quickstart demonstrates how to parse a BibTeX file, apply a predefined formatting style (or a custom one), and output the result to an HTML file using Pybtex's Python API. It creates a dummy `.bib` file, processes it, and then cleans up the generated files. For more complex use cases, especially with `.aux` files, the command-line interface or the `pybtex.Engine` might be more suitable."},"warnings":[{"fix":"For `.bst` styles, expect LaTeX output. For HTML/Markdown/plain text output, use Pythonic styles and configure the output backend explicitly (e.g., `pybtex --style-language python --output-backend html` or use `pybtex.database.output.html.Writer` in API).","message":"Pybtex uses two distinct 'engines' for bibliography formatting: a BibTeX engine (for `.bst` styles) and a Python engine (for Pythonic styles). The BibTeX engine can only output LaTeX, while the Python engine supports LaTeX, HTML, Markdown, and plain text. When using Pythonic styles, remember to specify `--style-language python` and `--output-backend <format>` via the command line or explicitly use the Python API for desired output formats.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Use `entry.persons['author']` for authors and `entry.fields['title']` for titles. Avoid `entry[0]` as `Entry` objects do not support indexing.","message":"When working with `BibliographyData` `Entry` objects in the Python API, access person-related fields (authors, editors, etc.) through the `entry.persons` dictionary, not `entry.fields`. `entry.fields` contains general string fields, while `entry.persons` holds a list of `Person` objects for each role. Additionally, `Entry` objects are not directly indexable.","severity":"gotcha","affected_versions":"All versions"},{"fix":"When using Pybtex programmatically, anticipate and handle `PybtexError` exceptions. For command-line use, the `--strict` option can be used to make warnings into errors.","message":"Pybtex's strict mode behavior differs between command-line usage and library usage. By default, command-line execution prints warnings to stderr for compatibility with BibTeX, whereas programmatic use as a library will raise exceptions on all errors. This can lead to unexpected crashes if not handled.","severity":"gotcha","affected_versions":"0.18+"},{"fix":"Consult the latest Pybtex documentation for the recommended API when reading/writing data and creating custom styles, especially for `pybtex.richtext` objects.","message":"The API for reading and writing bibliography data has evolved. Earlier methods like `Entry.get_field()` were deprecated. New API for rich text and general data handling was introduced around versions 0.19 and 0.22.","severity":"deprecated","affected_versions":"< 0.22"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}