[Tex/LaTex] Can we import Python file in latex

python

eg. Let say we have a python file songbook-makeindex.py which required input as .xsd file extension and give output as .sbx file extension, which needs to be run inside the latex file.

\documentclass{article}

{here some required packages}

\begin{document}

songbook-makeindex.py english_auth.sxd > english_auth.sbx

songbook-makeindex.py english_title.sxd > english_title.sbx

\end{document}

Best Answer

It could be a single file for all? I mean LaTeX code plus python code plus python output all mixed in one file with the .Rnw extension.

If so, you must convert it to .tex with R and knitr and then compile the LaTeX file as usual, or munch better, left to Rstudio to take care of the whole process.

Example:

mwe

% For .tex output use:  
% Rscript -e "library(knitr); knit('filename.Rnw')" 
\documentclass[a5paper]{article}
\setlength\parindent{0pt}
\begin{document}

This is not just verbatim text, but  executable python code:
<<test1, echo=T, eval=FALSE, engine='python'>>=
x = 'hello, python world!'
print(x)
print(x.split(' '))
@

And this the phython ouput of the above code from \textsc{this} file: 
<<test2, echo=F, comment="", engine='python'>>=
<<test1>>
@

All inside only a \LaTeX\ (R noweb) file.   

\end{document}