BibTeX Parser

1.4.4 · active · verified Mon Apr 13

bibtexparser is a Python 3 library for parsing and writing BibTeX files. It provides a simple API to read, modify, and dump BibTeX data. The current stable version is 1.4.4, with active development on a 2.0.0 beta series introducing significant changes. Releases are somewhat irregular, with bugfix and feature releases for the 1.x branch and active development on 2.x.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to read a BibTeX file, access and modify entries within the `BibDatabase` object, and then write the data back to a new file. It covers both reading with `bibtexparser.read_file` and writing with `bibtexparser.dump`, as well as direct manipulation of `BibDatabase` objects.

import bibtexparser
from bibtexparser.bibdatabase import BibDatabase
from bibtexparser.bparser import BibTexParser
import os

# Create a dummy .bib file for demonstration
dummy_bib_content = """
@article{doe2023example,
  title={An Example Article},
  author={Doe, John and Smith, Jane},
  journal={Journal of Examples},
  year={2023},
  volume={1},
  number={1},
  pages={1-10}
}
@book{smith2022guide,
  title={A Guide to Everything},
  author={Smith, Jane},
  publisher={Example Press},
  year={2022}
}
"""

# Ensure the file exists for the example
with open('temp_references.bib', 'w') as f:
    f.write(dummy_bib_content)

# 1. Read a BibTeX file
with open('temp_references.bib') as bibtex_file:
    # Using the default parser
    bib_database = bibtexparser.read_file(bibtex_file)

print("--- Parsed Database ---")
for entry in bib_database.entries:
    print(f"ID: {entry['ID']}, Title: {entry.get('title')}")

# 2. Access and modify entries
if bib_database.entries:
    first_entry = bib_database.entries[0]
    print(f"\nFirst entry authors: {first_entry.get('author')}")
    first_entry['note'] = 'Added by example'
    print(f"First entry new note: {first_entry.get('note')}")

# 3. Create a new BibDatabase and add an entry
new_bib = BibDatabase()
new_bib.entries.append({
    'ID': 'example2024new',
    'ENTRYTYPE': 'misc',
    'title': 'New Entry Added',
    'year': '2024'
})

# 4. Dump (write) to a new BibTeX file
with open('output.bib', 'w') as output_file:
    bibtexparser.dump(new_bib, output_file)

print("\nNew entry saved to output.bib")

# Clean up dummy files
os.remove('temp_references.bib')
os.remove('output.bib')

view raw JSON →