Home > Software engineering >  Iterate a function for each column needed from Excel dataframe
Iterate a function for each column needed from Excel dataframe

Time:05-25

I have script below that I'd like to read from a list that determines what columns to print, then merge PDF files using the data in the rows below said column.

My model_types list has elements like [model1], [mode2] and the dataframe looks like this:

   model1      model2    model3     
0  File1.pdf   File3.pdf File2.pdf
1  File2.pdf   nan.pdf   File2.pdf
2  File3.pdf   nan.pdf   File2.pdf

I'd like for it to read the models required from model_types then iterate the function for only those columns, producing each pdf document with the files from the rows in the column. model1 would have a PDF file with file1, file2 & file2where model2 would only have file3.

I get this error with the below:

FileNotFoundError: [Errno 2] No such file or directory: 'model1'

def merge_pdf(model_types: list[str]):
    merger = PdfFileMerger()
    for x in model_types:
        merger.append(x)
    merger.write("test{x}.pdf")
    merger.close()

merge_pdf(model_types)

CodePudding user response:

The issue is you are looping the data only column value not a row value so that only it showing model1 file not found error.

model_types is not a list it's data frame. In case if it's list means you need to convert the data frame using pandas

from PyPDF2 import PdfFileMerger
def merge_pdf(model_types):
    merger = PdfFileMerger()
    for col_name in model_types:
        for index, row in df.iterrows():
            merger.append(row[col_name])
    merger.write("test{x}.pdf")
    merger.close()

merge_pdf(model_types)
  • Related