将pdf分成两部分

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将pdf分成两部分相关的知识,希望对你有一定的参考价值。

我需要一个程序将每个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}" + "
" 
b =  "usepackage{pdfpages}" + "
"
c =  "egin{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 + "}" + "
"
    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分成两部分。

答案

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

enter image description here

documentclass{article}

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

egin{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}


ewlength{pagedim}% To store page dimensions, if necessary

egin{document}

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

end{document}

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

另一答案

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

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}
egin{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]{{{}}}
".format(i, file)
    middle += "includepdf[pages={},trim=400 0 0 0]{{{}}}
".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")

以上是关于将pdf分成两部分的主要内容,如果未能解决你的问题,请参考以下文章

ghostscript.net 可以将 PDF 文件分成多个部分吗?

PHP:将邮政编码分成两部分?

根据一列中的文本将一列数据分成两部分

如何将@RenderBody() 分成两部分

如何将列表分成两部分?

将列表分成两部分,它们的总和彼此最接近