3

I am using Ubuntu 22.04 LTS

I wanted to convert .rtf files to PDFs using Python.

I found this on stackoverflow:

import pypandoc

def convert_rtf_to_pdf(input_rtf, output_pdf):
    pypandoc.convert_file(input_rtf, 'pdf', outputfile=output_pdf)

if __name__ == "__main__":
    input_rtf_path = "Path to your RTF File"
    output_pdf_path = "path to your pdf file output with name /output.pdf"

    convert_rtf_to_pdf(input_rtf_path, output_pdf_path)

But I get this error:

RuntimeError: Invalid input format! Got "rtf" but expected one of these: commonmark, creole, csv, docbook, docx, dokuwiki, epub, fb2, gfm, haddock, html, ipynb, jats, jira, json, latex, man, markdown, markdown_github, markdown_mmd, markdown_phpextra, markdown_strict, mediawiki, muse, native, odt, opml, org, rst, t2t, textile, tikiwiki, twiki, vimwiki

Can I somehow add .rtf files to Ubuntu pandoc, then convert them to PDF?

Looking further, I found this, which shows rtf is available:

print(pypandoc.get_pandoc_formats())

(['commonmark', 'creole', 'csv', 'docbook', 'docx', 'dokuwiki', 'epub', 'fb2', 'gfm', 'haddock', 'html', 'ipynb', 'jats', 'jira', 'json', 'latex', 'man', 'markdown', 'markdown_github', 'markdown_mmd', 'markdown_phpextra', 'markdown_strict', 'mediawiki', 'muse', 'native', 'odt', 'opml', 'org', 'rst', 't2t', 'textile', 'tikiwiki', 'twiki', 'vimwiki'], ['asciidoc', 'asciidoctor', 'beamer', 'commonmark', 'context', 'docbook', 'docbook4', 'docbook5', 'docx', 'dokuwiki', 'dzslides', 'epub', 'epub2', 'epub3', 'fb2', 'gfm', 'haddock', 'html', 'html4', 'html5', 'icml', 'ipynb', 'jats', 'jats_archiving', 'jats_articleauthoring', 'jats_publishing', 'jira', 'json', 'latex', 'man', 'markdown', 'markdown_github', 'markdown_mmd', 'markdown_phpextra', 'markdown_strict', 'mediawiki', 'ms', 'muse', 'native', 'odt', 'opendocument', 'opml', 'org', 'pdf', 'plain', 'pptx', 'revealjs', 'rst', 'rtf', 's5', 'slideous', 'slidy', 'tei', 'texinfo', 'textile', 'xwiki', 'zimwiki'])

1 Answer 1

6

Your get_pandoc_formats() output contains two lists, only one of which has rtf in it. I suspect that rtf in this case is only supported as an output file format (since it is only in the second list), and not for input (which I'd guess is the first list). You can confirm this by running:

pandoc --list-input-formats | grep rtf
pandoc --list-output-formats | grep rtf

The commit adding a reader (input format support) for RTF is available in versions 2.14.2 and newer, but Ubuntu 22.04 only has 2.9.2. You can try using the binaries provided by Pandoc at https://github.com/jgm/pandoc/releases/latest (linked to from the Pandoc website), as Pandoc is a Haskell program, the final binary should be relatively independent of the base distro version.

2
  • Thanks! As you said, pandoc --list-output-formats | grep rtf shows rtf, but pandoc --list-input-formats | grep rtf shows nothing
    – Pedroski
    Apr 16 at 13:26
  • Thanks again! I solved the problem using a Python module called striprtf, which gets the text from rtf files. Apparently you can do that with re too, but that is above my pay grade! Once I have the text, the rest is easy! I use the module fitz for PDFs.
    – Pedroski
    Apr 17 at 4:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .