This script creates an LaTeX version of ask.tex, using pythontex. The python code includes subprocess
, and full python code runme.py
is,
#Python 3.4.3
import sys
import os
import re
import subprocess
import shutil
subprocess.call(['pdflatex', 'Ask.tex'])
subprocess.call([sys.executable, 'pythontex.py', 'Ask.tex'])
subprocess.call(['pdflatex', 'Ask.tex'])
and the LaTeX code ask.tex
is,
\documentclass{article}
\usepackage{ragged2e}
\usepackage[utf8]{inputenc}
\usepackage[letterpaper, top=0.5in, bottom=0.5in, left=1.55in, right=1.55in]{geometry}
\usepackage{multirow,array,varwidth,spreadtab,caption}
\usepackage[norule]{footmisc}
\usepackage{color, colortbl}
\usepackage{xcolor}
\usepackage{multicol}
\usepackage{pythontex}
\begin{document}
\begin{pycode}[][]
import mysql.connector
conn=mysql.connector.connect(user='root',password='oilwell',host='localhost',database='sqlpush1')
mycursor=conn.cursor()
query=("SELECT oil from results WHERE DATE(`date`) = '2029-01-01'")
mycursor.execute(query)
data= mycursor.fetchall()
for row in data:
print("{}".format(row[0]))
mycursor.close()
conn.close()
\end{pycode}
\begin{table}[ht]
\begin{center}
\begin{tabular}{|c|c|c|c|c|}
\multicolumn{5}{c}{\textbf{NET ENTITLEMENT FISH OIL MARKETS}}\\
\multicolumn{5}{c}{\textbf{AS OF DECEMBER 31, 2019}}\\\hline
%\rowcolor{Cyan}
\multicolumn{1}{|c|}{\multirow{2}{*}{Country}}&\multicolumn{1}{c|}{\multirow{2}{*}{Company}}&Proved Cooked&Proved Uncooked&Total Proved\\
%\rowcolor{Cyan}
\multicolumn{1}{|c|}{}&\multicolumn{1}{c|}{}&(T)&(T)&\cellcolor{blue!25}(T)\\\hline
ChUAN& \pyc{print("{}".format(row[0]))} &51,574&31,202&82,776\\\hline
IraW-West POINT & & 9,656 &57,981&67,637\\\hline
\multicolumn{1}{|c|}{\multirow{3}{*}{KAZSLSKDE}}& PKKR& 32,129 &5,174& 37,304 \\\cline{2-5}
\multicolumn{1}{|c|}{}&KOLKOL&9,718&3,447&13,165\\\cline{2-5}
\multicolumn{1}{|c|}{}&PKPK&424&0&424\\\hline
TOTAL CNNNO & & 103,501 & 97,804 & 201,306\\\hline
\end{tabular}
\end{center}
\label{tab:multicol}
\end{table}
\end{document}
Pythontex need 3-steps to compile the files, first run pdflatex
, then run 'pythontex file.py', and run pdflatex
again.
I put both runme.py
and ask.tex
under the folder of pythontex, which has the pythotex.py
file.
After I run runme.py
in IDLE mode, pdflatex could be executed, and cmd prompt windows pop up and finish without problem. But the problem is, I can not see pythontex.py
running, and that lead to no change in output pdf file.
I'm appreciated any comments and help on this problem. Thanks a lot.
[Updates] I edited the python code into,
import sys
import os
import re
import subprocess
import shutil
subprocess.call(['pdflatex', 'Ask.tex'])
try:
subprocess.call(['pythontex', 'Ask.tex'])
except:
subprocess.call(['pythontex.py', 'Ask.tex'])
subprocess.call(['pdflatex', 'Ask.tex'])
Again, pdflatex
could be executed, however, there is some error,
Traceback (most recent call last):
File "C:\Users\Cheng\Desktop\LaTeX Project\pythontex\runme2.py", line 10, in <module>
subprocess.call(['pythontex', 'Ask.tex'])
File "C:\Python34\lib\subprocess.py", line 537, in call
with Popen(*popenargs, **kwargs) as p:
File "C:\Python34\lib\subprocess.py", line 859, in __init__
restore_signals, start_new_session)
File "C:\Python34\lib\subprocess.py", line 1112, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Cheng Zhang\Desktop\LaTeX Project\pythontex\runme2.py", line 12, in <module>
subprocess.call(['pythontex.py', 'Ask.tex'])
File "C:\Python34\lib\subprocess.py", line 537, in call
with Popen(*popenargs, **kwargs) as p:
File "C:\Python34\lib\subprocess.py", line 859, in __init__
restore_signals, start_new_session)
File "C:\Python34\lib\subprocess.py", line 1112, in _execute_child
startupinfo)
OSError: [WinError 193] %1 is not a valid Win32 application
And that's new problem. Core problem, pythontex won't run.
After doing some research, I found this way could be solve the problem perfectly.
The key part is add,
shell=True
, otherwise Python won't recognize the statement.