如何使用Python导入(.tex文件),创建新的(.tex文件)并从导入的(.tex文件)附加新的(.tex文件)

2024-04-19 20:31:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我有几个(1000+).tex文件如下:

文件1.tex:

\documentclass[15pt]{article}
\begin{document}

Question1:

$f(x)=sin(x)$\\

Question2:

$f(x)=tan(x)$

\end{document}

File2.tex的结构类似:

\documentclass[15pt]{article}

\begin{document}

Question1:

$f(x)=cos(x)$\\


Question2:

$f(x)=sec(x)$\\

Question3:

$f(x)=cot(x)$

\end{document}

我想做的是编写一个Python脚本,允许我从file1.tex中选择问题1,从file2.tex中选择问题3,并用以下格式编译一个新的file3.tex文件(或PDF):

\documentclass[15pt]{article}
\begin{document}

Question1:
$f(x)=sin(x)$\\

Question2:
$f(x)=cot(x)$

\end{document}

我不介意我是否能在乳胶上做这种工作。我只是想用Python我最终可以创建一个GUI。你知道吗

到目前为止,我已经设法通过手动键入我想要的内容来读取/附加一个.tex文件,而不是创建某种系统,允许我将一个或多个.tex文件的特定部分“复制”到另一个.tex文件中。你知道吗


Tags: 文件articlesincos结构documentfile2end
1条回答
网友
1楼 · 发布于 2024-04-19 20:31:28

我用的正是你在file1和file2.tex中所用的,我在整个过程中留下了评论,而不是一步一步地解释。你知道吗

预处理

预处理包括创建一个xlsx文件,该文件的第一列将包含tex文件的所有名称。你知道吗

import os
import xlsxwriter

workbook = xlsxwriter.Workbook('Filenames.xlsx')
worksheet = workbook.add_worksheet("FileNames")
worksheet.write(0, 0, "NameCol")

path = os.getcwd()  # get path to directory
filecount = 1
for file in os.listdir(path):  # for loop over files in directory
    if file.split('.')[-1] == 'tex':  # only open latex files
        worksheet.write(filecount, 0, file)
        filecount += 1

workbook.close()

选择问题

现在你看右边的一个列表,就像我有你想要的问题一样。 Filenames.xlsx

后处理

现在我们可以运行xlsx文件并从中创建一个新的latex文件。你知道吗

import pandas as pd
import math
import os

# get data
allfileqs = []
df = pd.read_excel('Filenames.xlsx')
for row in df.iterrows():
    tempqs = []
    for i in range(len(row[1].values) - 1):
        if math.isnan(row[1].values[i + 1]):
            continue
        else:
            tempqs.append(int(row[1].values[i + 1]))
    allfileqs.append(tempqs)
print(allfileqs)
allfileqs = [["Question" + str(allfileqs[i][j]) + ':' for j in range(len(allfileqs[i]))] for i in range(len(allfileqs))]

starttex = [r'\documentclass[15pt]{article}', r'\begin{document}']
endtex = [r'\end{document}']
alloflines = []


path = os.getcwd()  # get path to directory
for file in os.listdir(path):  # for loop over files in directory
    if file.split('.')[-1] == 'tex':  # only open latex files
        lf = open(file, 'r')
        lines = lf.readlines()
        # remove all new lines, each item is on new line we know
        filt_lines = [lines[i].replace('\n', '') for i in range(len(lines)) if lines[i] != '\n']
        alloflines.append(filt_lines)  # save data for later
        lf.close()  # close file
# good now we have filtered lines
newfile = []
questcount = 1
for i in range(len(alloflines)):
    for j in range(len(alloflines[i])):
        if alloflines[i][j] in allfileqs[i]:
            newfile.append("Question" + str(questcount) + ":")
            newfile.append(alloflines[i][j + 1])
            questcount += 1
# okay cool we have beg, middle (newfile) and end of tex
newest = open('file3.tex', 'w')  # open as write only
starter = '\n\n'.join(starttex) + '\n' + '\n\n'.join(newfile) + '\n\n' + endtex[0]
for i in range(len(starter)):
    newest.write(starter[i])
newest.close()

相关问题 更多 >