?以前写了一个遍历文件夹下所有文件夹中pdf文件并合并成一个的代码
folder=os.listdir(dir_path) #遍历文件夹
#处理
def merge_pdf(name,file_all,target_path):
total=PdfFileWriter()
totalPages=0
dc={}
target_name=name+'.pdf'
new=os.path.join(target_path,target_name)
for pdf_file in file_all:
print(pdf_file)
#读取pdf文件
pic=open(pdf_file,'rb')
dc[pdf_file]=pic
input_pdf=PdfFileReader(pic)
#获取pdf总页数
pagecount=input_pdf.getNumPages()
print(pagecount)
totalPages+=pagecount
#遍历每个pdf文件
for iPage in range(pagecount):
#将每一页合并过去
total.addPage(input_pdf.getPage(iPage))
with open(new,'wb') as outputfile:
total.write(outputfile)
pic.close()
os.remove(pdf_file)
print(totalPages,'份pdf文件合并中......')
#遍历操作,根据需求不要或更改
for f in folder:
a,b=(os.path.splitext(f))
if not b: #找到文件夹
source_path=(os.path.join(dir_path,f))
name=a
if os.listdir(source_path)==[]: #无甚么大用的判断
continue
else:
file_all=[os.path.join(source_path,filename)\
for dirpath,folder,files in os.walk(source_path)\
for filename in files if filename[-4:]=='.pdf'] #遍历子级文件夹
print(file_all)
merge_pdf(name,file_all,target_path)
# break
print('合并完毕!')
|