Package: pandadoc
pandadoc: lightweight pandoc wrapperAn extremely lightweightpandocwrapper for Python 3.8+.Its features:Supports conversion between all formats thatpandocsupports -
markdown, HTML, LaTeX, Word, epub, pdf (output),and more.Output to rawbytes(binary formats - e.g. PDF), tostrobjects
(text formats - e.g. markdown), or to file (any format).pandocerrors are raised as (informative) exceptions.Full flexibility of thepandoccommand-line tool, and the same syntax. (See thepandoc manualfor more information.)Getting Started GuideInstallationFirst, ensurepandocis on yourPATH.
(In other words,install pandocand add it to
yourPATH.)Then installpandadocfromPyPI:$python-mpipinstallpandadocThat’s it.UsageConvert a webpage to markdown, and store it as a pythonstr:>>>importpandadoc>>>input_url="https://example.com/">>>example_md=pandadoc.call_pandoc(...options=["-t","markdown"],files=[input_url]...)>>>print(example_md)<div># Example DomainThisdomainisforuseinillustrativeexamplesindocuments....Now convert the markdown to RTF, and write it to a file:>>>rtf_output_file="example.rtf">>>pandadoc.call_pandoc(...options=["-f","markdown","-t","rtf","-o",rtf_output_file],...input_text=example_md,...)''Notice thatcall_pandocreturns an empty string''when a file output is used.
Looking at the output file:{\pard \ql \f0 \sa180 \li0 \fi0 \outlinelevel0 \b \fs36 Example Domain\par}
{\pard \ql \f0 \sa180 \li0 \fi0 This domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission.\par}
{\pard \ql \f0 \sa180 \li0 \fi0 {\field{\*\fldinst{HYPERLINK "https://www.iana.org/domains/example"}}{\fldrslt{\ul
More information...
}}}
\par}Convert this RTF document to PDF, using xelatex with a custom character set,
and store the result as rawbytes:>>>raw_pdf=pandadoc.call_pandoc(...options=["-f","markdown","-t","pdf","--pdf-engine","xelatex","--variable-mainfont","Palatino"],...files=[rtf_output_file],...decode=False,...)Note that PDF conversion requires a
“PDF engine”
(e.g. pdflatex, latexmk etc.) to be installed.Now you can send those raw bytes over a network, or write them to a file:>>>withopen("example.pdf","wb")asf:...f.write(raw_pdf)...>>># FinishedYou can find morepandocexampleshere.ExceptionsIfpandocexits with an error, an appropriate exception is raised (based on theexit code):>>>pandadoc.call_pandoc(...options=["-f","markdown","-t","zzz"],# non-existent format...input_text=example_md,...)Traceback(mostrecentcalllast):...pandadoc.exceptions.PandocUnknownWriterError:Unknownoutputformatzzz>>>isinstance(pandadoc.exceptions.PandocUnknownWriterError(),pandadoc.PandocError)TrueYou can find a full list of exceptions in thepandadoc.exceptionsmodule.ExplanationThepandoccommand-line tool works like this:pandoc [OPTIONS] [FILES]In addition to theOPTIONS(documented here),
you can provide either someFILES, or some input text (viastdin).Thecall_pandocfunction ofpandadocworks in a similar way:Theoptionsargument contains a list of pandoc options.
E.g.["-f","markdown","-t","html"].Thefilesargument is a list of file paths (or absolute URIs).
E.g.["path/to/file.md","https://www.fsf.org"]Theinput_textargument is used as text input to pandoc.
E.g.# Simple Doc\n\nA simple markdown document\n.Thetimeoutanddecodearguments are used to control whether thepandocprocess times out, and whether the result should be decoded to astr(Trueby default).Bugs/RequestsPlease use theGitHub issue trackerto submit bugs or request features.Feedback is always appreciated.LicenseDistributed under theMIT license.