我需要将一个文件夹中的PDF文件合并成一个文件,但是它们必须按照特定的顺序进行合并。文件名的示例如下:
WR_Mapbook__1.pdf
WR_Mapbook__1a.pdf
WR_Mapbook__2.pdf
WR_Mapbook__2a.pdf
WR_Mapbook__3.pdf
WR_Mapbook__3a.pdf
etc...
他们在Windows资源管理器中排序的方式是我需要将它们添加到单个文件中的方式。然而,我的脚本首先添加所有带有“a”的文件,然后才是没有“a”的文件。为什么会这样?如何按照我想要的方式排序文件并添加它们?
请参见下面的代码。谢谢!
from pyPdf import PdfFileWriter, PdfFileReader
import glob
outputLoc = "K:\\test\\pdf_output\\"
output = PdfFileWriter()
pdfList = glob.glob(r"K:\test\lidar_MB_ALL\*.pdf")
pdfList.sort
print pdfList
for pdf in pdfList:
print pdf
input1 = PdfFileReader(file(pdf, "rb"))
output.addPage(input1.getPage(0))
# finally, write "output" to document-output.pdf
outputStream = file(outputLoc + "WR_Imagery_LiDar_Mapbook.pdf", "wb")
output.write(outputStream)
print ("adding " + pdf)
outputStream.close()