# Easy way to include multiple image files from file directory through Python code?

I am trying to include an unknown amount of image files with unknown file names in a document. My code is inspired by a comment on this answer: https://tex.stackexchange.com/a/53465/206040

\documentclass[12pt]{article}

\usepackage{graphicx}

\usepackage{pythontex}

\begin{document}
Pictures:
\begin{pycode}
import os
directory = "".""
extension = ".png"
files = [file for file in os.listdir(directory) if file.lower().endswith(extension)]

for file in files:
print r"\begin{figure}[!ht]"
print r"\centering"
print r"\includegraphics[width=10cm,height=10cm]{%s}" % file
print r"\caption{File %s}" % file
print r"\label{Serie}"
print r"\end{figure}"
\end{pycode}

\end{document}

The code is working but the images are not included in my pdf. How do I solve this or is there a better / easier way to do this?

By the way: I've wanted to use the pythontex package as it does not require using the --enable-write18 option? But I also don't know if there is an even better way.

• Your code looks OK. Did you enable the --shell-escape flag during compilation? This flag has to be set as TeX requires shell access in order to interact with Python. --enable-write18 is just a similar thing. Dec 26 '20 at 0:39
• Yes, I have tried txs:///pdflatex, txs:///pdflatex --shell-escape, and txs:///pdflatex --enable-write18 in TexStudio as compilation commands. It runs fine, but the images are not included in my pdf file. Dec 27 '20 at 10:18

## 1 Answer

Here's the solution I came up with. I have taken the liberty to introduce slight changes in your code:

\documentclass[12pt]{article}

\usepackage{graphicx}
\usepackage{pythontex}

\begin{document}

Pictures:

\begin{pycode}
import os
import textwrap

directory = '.'
extension = '.png'

files = [fn for fn in os.listdir(directory) if fn.lower().endswith(extension)]

figs = []
for count, filename in enumerate(files, 1):
fixedfn = filename.replace(r'_', r'\_')
fig = fr'''
\begin{{figure}}[!ht]
\centering
\includegraphics[height=4cm]{{{filename}}}
\caption{{File {fixedfn}}}
\label{{Serie{count}}}
\end{{figure}}
'''
figs.append(textwrap.dedent(fig))

print(''.join(figs))
\end{pycode}

\end{document}

To use this code, install pygments using pip install pygments in your Python environment.

Compiling is done in the following order:

1. Compile LaTeX file (e.g. in TeXStudio)
2. Navigate in the directory of your LaTeX file using cd and use pythontex documentname.tex in the terminal where "documentname" is the name of the LaTeX file.
3. Compile LaTeX file (e.g. in TeXStudio) again, pictures should now be visible!

Alternatively you can just use the following command as your standard compiler in TeXStudio:

txs:///pdflatex | pythontex %.tex | txs:///pdflatex

When run on my machine, the Python code produces the following LaTeX source code:

\begin{figure}[!ht]
\centering
\includegraphics[height=4cm]{1200px-LaTeX_project_logo_bird.svg.png}
\caption{File 1200px-LaTeX\_project\_logo\_bird.svg.png}
\label{Serie1}
\end{figure}

\begin{figure}[!ht]
\centering
\includegraphics[height=4cm]{campusVigo_vistas01.png}
\caption{File campusVigo\_vistas01.png}
\label{Serie2}
\end{figure}

\begin{figure}[!ht]
\centering
\includegraphics[height=4cm]{opengraph-icon-200x200.png}
\caption{File opengraph-icon-200x200.png}
\label{Serie3}
\end{figure}

which results in:

• Thanks for your helpful answer! After some testing, I finally made it work using your code. I had to install pygments using pip install pygments in my Python environment. Also I had to compile in TeXStudio first, then navigate to the folder of the tex file in the terminal using the cd command (change directory) and then use the command pythontex documentname.tex in the terminal where "documentname" is the name of the tex file and then compile in TeXStudio again. Also see this answer (just ignore the \printpythontex command): tex.stackexchange.com/a/276840/206040 Apr 14 at 10:59
• Also one can use txs:///pdflatex | pythontex %.tex | txs:///pdflatex as the standard compiler in TeXStudio. Apr 14 at 11:29