使用Pandas数据框和PyPDF2组合多个PDF

时间:2019-01-25 14:30:18

标签: python pandas pypdf2

我试图通过首先遍历数据帧然后遍历文件路径来组合两个PDF。

重命名和移动功能有效,但是该程序只组合了我列表中的前两个pdf。下面是我的代码:

#set paths
path = 'C:\\Users\\sferrier\\Desktop\\test_1'
dest = 'C:\\Users\\sferrier\\Desktop\\test_1_output'
os.chdir(path)

#creater empty lists to later append to 
slipsheet = []
document = []
new_file = [] 

#load the dataframe
df = pd.read_excel('slip_sheet_exs.xlsx', columns = ['slipsheet', 'filename'])

#iterate through excel and add columns to a list
for index, row in df.iterrows():
    slipsheet.append(row['slipsheet'])
    document.append(row['filename'])

#define merger and i for later use in the for loop 
merger = PdfFileMerger()
i = 0 

#combine the pdfs and move them
for files in os.dir():
    merger.append(slipsheet[i]) 
    merger.append(document[i])
    merger.write(slipsheet[i] + " - " + document[i])
    new_file = path + '\\' + slipsheet[i] + " - " + document[i]
    shutil.move(new_file, dest)
    i= i+1
    if slipsheet == slipsheet[i-1]:
         continue 
    elif i == len(df):
         break

以下内容一定有问题,但我不确定是什么

for files in os.dir():
    merger.append(slipsheet[i]) 
    merger.append(document[i])
    merger.write(slipsheet[i] + " - " + document[i])

0 个答案:

没有答案