子进程 pythontex 不会工作

subprocess pythontex wont work

此脚本使用 pythontex 创建 ask.tex 的 LaTeX 版本。 python 代码包括 subprocess,完整的 python 代码 runme.py 是,

#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'])

而 LaTeX 代码 ask.tex 是,

\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}

Python 需要 3 个步骤来编译文件,首先是 运行 pdflatex,然后是 运行 'pythontex file.py',然后是 运行 pdflatex再次.

我把runme.pyask.tex都放在pythontex的文件夹下,里面有pythotex.py文件

我在IDLE模式运行 runme.py后,可以执行pdflatex,弹出cmd提示符windows,顺利完成。 但问题是,我看不到 pythontex.py 运行ning,这导致输出的 pdf 文件没有变化。

非常感谢对此问题的任何评论和帮助。非常感谢。

[更新] 我将 python 代码编辑为

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'])

同样,pdflatex可以执行,但是,有一些错误,

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

这是新问题。 核心问题,pythontex不会运行。

经过一番研究,发现这种方式可以完美解决问题。

#Python 3.4.3
import sys
import os
import re
import subprocess
import shutil


subprocess.call(['pdflatex', 'ask.tex'])
try:
    subprocess.call(['pythontex', 'ask.tex'], shell=True)
except:
    subprocess.call(['pythontex.py', 'ask.py'], shell=True)
subprocess.call(['pdflatex', 'ask.tex'])

关键部分是add,shell=True,否则Python无法识别该语句。