此脚本使用pythontex创建一个laTeX版本的ask.tex。 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}
Pythontex需要3个步骤来编译文件,首先运行pdflatex
,然后运行'pythontex file.py',然后再次运行pdflatex
。
我将runme.py
和ask.tex
放在pythontex的文件夹下,该文件夹包含pythotex.py
文件。
在IDLE模式下运行runme.py
后,可以执行pdflatex,并弹出cmd提示窗口并完成没有问题。 但问题是,我看不到pythontex.py
正在运行,导致输出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无法运行。
答案 0 :(得分:0)
经过一番研究,我发现这种方式可以很好地解决问题。
#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不会识别该语句。