Function to call a extern command as a filter

Rhodri James rhodri at kynesim.co.uk
Wed Sep 25 07:27:29 EDT 2019


On 25/09/2019 12:04, Ulrich Goebel wrote:
> Hello,
> 
> what I want:
> 
> I want a function which converts a markdown-formatted string to a 
> latex-formatted string
[snip]
> I have a extern tool pandoc which does exactly what I want, but it
> works on files. This tool is able to work as a pipe, so it uses
> the standard input and standard outpu.
> 
> What I could do:
> 
> def markdown_to_latex (m : string)
>      write the string m to a file
>      call pandoc to work on that file
>      read the outputfile into the string l
>      return (l)
> 
> What would be nice:
> 
> I would like to avoid the extra steps writing an reading extern files.

subprocess is your friend here.  Something like:

import subprocess
def mardown_to_latex(markdown_string):
   latex = subprocess.run(cmd_string_for_pandoc,
                          input=markdown_string,
                          string=True,
                          capture_output=True)
   return latex.output

The above is completely untested and not in the least bit robust, but 
that's the area of the standard library you should be looking at.

-- 
Rhodri James *-* Kynesim Ltd



More information about the Python-list mailing list