将pdf分成两部分

问题描述 投票:1回答:2

我需要一个程序将每个PDF页面分成两部分(左,右)。所以我制作了这段代码,但由于某些原因它没有抓住标题的图像。在尝试使用其他书籍时,它也无法正常工作。

import os

#Info that i collect to know the numbers of pages and the pdf file name
number = int(input("Number os pages: " ))
file = input("Name of the file: " )
file = str(file) + ".pdf"


text = open("for_the_latex.txt","w")

#Putting the first part of the latex document

a =  "\documentclass{article}" + "\n" 
b =  "\\usepackage{pdfpages}" + "\n"
c =  "\\begin{document}"

text.write(a)
text.write(b)
text.write(c)

#This is the core of the program
#It basically write in a text document to include the pdf for each page

for i in range(1,number +1): 
    a = "\includepdf[pages=" + str( i) + ",trim=0 0 400 0]{" + file + "}" + "\n"
    text.write(a)


#Writing the finish part
quatro = "\end{document}"

text.write(quatro)

text.close()

#renaming to .tex
os.rename("for_the_latex.txt", "divided.tex")

#activating the latex
os.system("pdflatex divided.tex")

错误在哪里?

enter image description here

我想把PDF分成两部分。

python latex pdflatex
2个回答
1
投票

考虑以下包含6个页面的最小文档(称为example-document.pdf),每个页面按颜色和数字精确分成两半:

enter image description here

\documentclass{article}

\usepackage[paper=a4paper,landscape]{geometry}
\usepackage{pdfpages}

\begin{document}

% http://mirrors.ctan.org/macros/latex/contrib/mwe/example-image-a4-numbered.pdf
\includepdf[pages={1-2},nup=2]{example-image-a4-numbered.pdf}
\includepdf[pages={3-4},nup=2]{example-image-a4-numbered.pdf}
\includepdf[pages={5-6},nup=2]{example-image-a4-numbered.pdf}
\includepdf[pages={7-8},nup=2]{example-image-a4-numbered.pdf}
\includepdf[pages={9-10},nup=2]{example-image-a4-numbered.pdf}
\includepdf[pages={11-12},nup=2]{example-image-a4-numbered.pdf}

\end{document}

我们的想法是将这些分成12页的文档。这是LaTeX的代码:

enter image description here

\documentclass{article}

\usepackage[paper=a4paper]{geometry}
\usepackage{pdfpages,pgffor}

\newlength{\pagedim}% To store page dimensions, if necessary

\begin{document}

\foreach \docpage in {1,...,6} {
  \settowidth{\pagedim}{\includegraphics[page=\docpage]{example-document.pdf}}% Establish page width
  \includepdf[pages={\docpage},trim=0 0 .5\pagedim{} 0,clip]{example-document.pdf}% Left half
  \includepdf[pages={\docpage},trim=.5\pagedim{} 0 0 0,clip]{example-document.pdf}% Right half
}

\end{document}

没有必要在每个页面中读取并确定其宽度(存储在\pagedim中),但我不确定您的页面是否可能具有不同的大小。


0
投票

正如评论中提到的,如果我理解你的问题,我不太确定。由于我可以执行您的程序并且它只包含初始文档的左侧部分,因此我稍微修改了代码。

import os

#Info that i collect to know the numbers of pages and the pdf file name
number = int(input("Number of pages: "))
file = input("Name of the file: ")
file = str(file) + ".pdf"

text = open("divided.tex","w")

#Putting the first part of the latex document
header ='''
\\documentclass{article}
\\usepackage{pdfpages}
\\begin{document}
'''

#This is the core of the program
#It basically write in a text document to include the pdf for each page

middle=''
for i in range(1,number +1):
    middle += "\includepdf[pages={},trim=0 0 400 0]{{{}}}\n".format(i, file)
    middle += "\includepdf[pages={},trim=400 0 0 0]{{{}}}\n".format(i, file)

#Writing the finish part
quatro = "\end{document}"

text.write(header)
text.write(middle)
text.write(quatro)
text.close()

#activating the latex
os.system("pdflatex divided.tex")
© www.soinside.com 2019 - 2024. All rights reserved.