A_001.pdf
A_002.pdf
A_003.pdf
B_001.pdf
B_002. pdf
B_003.pdf
B_004.pdf
По сути, мне нужен один PDF-файл для A (в PDF-файле будет 3 страницы) и один PDF-файл для B (в PDF-файле будет 4 страницы) страницы). _001 и т. д. должны быть номером страницы. Мой текущий скрипт Python выводит A.pdf и B.pdf, но включает страницы как из A, так и из B.
Код: Выделить всё
import PyPDF2, os
from PyPDF2 import PdfFileReader, PdfFileWriter, PdfFileMerger
from pathlib import Path
single_file_dir = r'Y:\Python\Single_PDFs'
binder_file_dir = r'Y:\Python\Combined_PDFs'
# get list of all files in the single PDF directory
single_file_list = []
for file in os.listdir(single_file_dir):
if file.endswith(".pdf"):
single_file_list.append(single_file_dir + "\\" + file)
print(single_file_list)
# get the file names for the output multi page pdfs
file_name_list = []
for file in single_file_list:
name = os.path.basename(file)
new_name = name[:-8]
file_name_list.append(new_name)
unique_file_name_list = list(set(file_name_list))
merger = PdfFileMerger()
print(unique_file_name_list)
#try to match input single file name to output file name
for file in single_file_list:
for name in unique_file_name_list:
if name in file:
merger.append(file)
merger.write(binder_file_dir + "\\" + name + ".pdf")
Подробнее здесь: https://stackoverflow.com/questions/790 ... fferent-nu