Search code examples
pythonpandocrestructuredtextbibtex

pypandoc + pandoc-citeproc to convert bib to rst


I have a .bib file with references and I would like to render that file as restructuredtext (in other words, my input is a file references.bib and my desired is file references.rst). That conversion should be done with a Python script, so I tried to use pypandoc (version 0.9.7, with pandoc version 1.13.1) in combination with the pandoc-citeproc filter. pypandoc and pandoc are installed correctly, e.g. the following works

import pypandoc
print(pypandoc.get_pandoc_version())

My first idea is to use some kind of nocite* command in a tex or md file and convert that. The (adapted) example from the pandoc-citeproc issue tracker works when used from the command line:

pandoc references.tex  -o references.rst --bibliography references.bib

But I did not succeed to do the same from python with pypandoc, it is especially unclear to me how to pass extra arguments, e.g. change ibid behavior or specify a CSL style (changing the biblatex style seems to have no effect).

Also, I do not really like the workaround with the .tex file. Can I somehow skip that part?


Solution

  • You can use citeproc-py for that. You will need to:

    1. create a citeproc.CitationsStyleBibliography, passing the citation style, a bibtex citation source and a reStructuredText formatter to it;
    2. register each element from your bibliographic source;
    3. print the bibliography.

    You can find examples for these steps in the examples/ directory of citeproc-py, or in bibgen source code. Note that bibgen allows you to directly include a bibliography in a reStructuredText document (including Sphinx sources), see this example.