32

I'm developing using Python and Django for a website. I want to take a BibTex entry and output it in a view in 3 different formats, MLA, APA, and Chicago. Is there a library out there that already does this or am I going to have to manually do the string formatting?

War Gravy
  • 1,543
  • 3
  • 20
  • 32

4 Answers4

31

There are the following projects:

If you need complex parsing and output, Pybtex is recommended. Example:

>>> from pybtex.database.input import bibtex
>>> parser = bibtex.Parser()
>>> bib_data = parser.parse_file('examples/foo.bib')
>>> bib_data.entries.keys()
[u'ruckenstein-diffusion', u'viktorov-metodoj', u'test-inbook', u'test-booklet']
>>> print bib_data.entries['ruckenstein-diffusion'].fields['title']
Predicting the Diffusion Coefficient in Supercritical Fluids

Good luck.

miile7
  • 2,547
  • 3
  • 23
  • 38
tatlar
  • 3,080
  • 2
  • 29
  • 40
  • Note that BibtexParser doesn't actually know how to parse author/editor name lists. I would say `pybtex` is definitely the way to go. – Juan A. Navarro May 26 '23 at 14:13
12

Having tried them, all of these projects are bad, for various reasons: terrible APIs, bad documentation, and a failure to parse valid BibTeX files. The implementation you want doesn't show up in most Google searches, from my own searching: it's biblib. This text from the README should sell it:

There are a lot of BibTeX parsers out there. Most of them are complete nonsense based on some imaginary grammar made up by the module's author that is almost, but not quite, entirely unlike BibTeX's actual grammar. BibTeX has a grammar. It's even pretty simple, though it's probably not what you think it is. The hardest part of BibTeX's grammar is that it's only written down in one place: the BibTeX source code.

matt
  • 189
  • 1
  • 5
  • Not in my experience. biblib's `bib2bib` example will not output valid BibTeX for [this paper's BibTeX](https://ieeexplore.ieee.org/document/7546521) – Alin Tomescu Oct 05 '19 at 15:09
  • @AlinTomescu you may be using the wrong library. The `biblib` linked above is not published on PyPI at time of writing. See: https://github.com/aclements/biblib/issues/4 – Radu Jul 20 '20 at 17:17
  • I see. In the end, I ended up using bibtexparser with more success: https://bibtexparser.readthedocs.io/en/master/bibtexparser.html – Alin Tomescu Jul 21 '20 at 20:56
  • I tried using @matt's biblib and found that it errored on files that bibtex and biblatex accepted. Ther eis no way to get around parse errors? – vy32 Jan 09 '23 at 19:34
4

The accepted answer of using pybtex is fraught with danger as Pybtex does not preserve the bibtex format of even simple bibtex files. (https://bitbucket.org/pybtex-devs/pybtex/issues/130/need-to-specially-represent-bibtex-markup)

Pybtex is therefore losing bibtex information when reading and re-writing a simple .bib file without making any changes. Users should be very careful following the recommendations to use pybtex.

I will try biblib as well and report back but the accepted answer should be edited to not recommend pybtex.

Edit: I was able to import the data using Bibtex Parser, without any loss of data. However, I had to compile from https://github.com/sciunto-org/python-bibtexparser as the version installed via pip was bugged at the time. Users should verify that pip is getting the latest version.

As for exporting, once the data has been imported via BibTex Parser, it's in a dictionary, and can be exported as the user desires. BibTex Parser does not have built in functions for exporting in common formats. As I did not need this functionality, I didn't specifically test it. However, once imported into a dictionary, the string output can be converted to any citation format rather easily.

Here, pybtex and a custom style file can help. I used the style file provided by the journal and compiled in LaTeX instead, but PyBtex has python style files (but also allows ingesting .sty files). So I would recommend taking the Bibtex Parser input and transferring it to PyBtex (or similar) for outputting in a certain style.

  • I was able to import without loss by using Bibtex Parser. After that point, any style file, custom definition, or package can be used to write out a string with the bibliography style one wants. I did not test this latter part in detail, however Pybtex seems to be useful here. – TheDragonReborn Sep 05 '19 at 17:16
  • none of those projects is active – Wang Apr 13 '22 at 19:58
0

The closest thing I know of is the pybtex package

David Hall
  • 515
  • 6
  • 13