发布于 2019-05-03 14:02:35
我自己解决了问题。编写了python代码来处理它。检索PageLabels检索标签本身,该标签本身可能是数字的,也可能不是数字的,以及所述标签开始的相应索引。我提取标签的开始索引,并假设节或标签的结束在下一个标签/节开始之前立即发生1页。
#!/usr/bin/python
from PyPDF2 import PdfFileWriter, PdfFileReader
import numpy as np
def printf(format, *values):
print(format % values )
with open("in.pdf", "rb") as in_f:
input1 = PdfFileReader(in_f)
output = PdfFileWriter()
numPages = input1.getNumPages()
# The label indices occur @ even locations - generate array of form [0, 2, 4, 6, ...]
indices = np.array(np.arange(0,np.shape(input1.trailer["/Root"]["/PageLabels"]["/Nums"])[0],2))
# Assume end of preceding label = start of next label - 1
pageIndices = np.array(input1.trailer["/Root"]["/PageLabels"]["/Nums"])[indices] - 1
# ignore the first index which is now a -1
pageIndices = pageIndices[1:]
# there may be extra pages right after the start of the last label - add them
pageIndices = np.append(pageIndices, np.arange(pageIndices[-1]+1, numPages))
for _, v in enumerate(pageIndices):
page = input1.getPage(v)
output.addPage(page)
with open("out.pdf", "wb") as out_f:
output.write(out_f)https://askubuntu.com/questions/1140224
复制相似问题