Python Forum
Python PDF merging from an excel pandas for loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python PDF merging from an excel pandas for loop
#1
I have an excel sheet, with some dropdown lists. (Working) Now i'm in Python, trying to read the date from the excel sheet (xlsx file) and reading the data into a for loop (Also working)

I have 3 column with a name, the name ref. to a pdf file, all pdf files are located the same place. I need to merge the 3 random PDF files into one.

So I can see i can use PyPDF2... But how can I do it in my for loop, so it will read the 3 values row by row and merge the files into one PDF, row by row?

My code is this ATM and i'm getting the right values from the xlsx sheet row by row.

import os
import pandas as pd
from PyPDF2 import PdfFileMerger

data = pd.read_excel(r'Resources\liste.xlsx', sheet_name='Ark1', skiprows=3)
dataread = pd.DataFrame(data)
for index, row in dataread.iterrows():
    print(index, row)
I can see (ref to PyPDF2)how to get the files into to PyPDF2, my problem is that i'm getting 4 values from the excel sheet row by row. ex. Value1=u6AB, Value2=FUO0002, Value3=FUO0004, Value4=u34_driblinger

From that I then have a location c:\users\myuser\document\master\pdf\ in here i have u6ABx.pdf, FUO0002_xxxxxxx.pdf and FUO0004_xxxxxxx.pdf these 3 files I want to merge into u34_driblinger.pdf

How can I do that from the ex. from the link, like:

for index, row in dataread.iterrows():
    print(index, row)
    try:
    # if doc exist then merge
        if os.path.exists(row):
            input = PyPDF2.PdfFileReader(open(row, 'rb'))
            merger.append((input))
        else:
            print(f"problem with file {row}")

    except:
        print("cant merge !! sorry")
    else:
        print(f" {row} Merged !!! ")

merger.write("Merged_doc.pdf")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pandas keep existing format of Excel AlphaInc 2 881 Jan-11-2024, 03:44 AM
Last Post: plonkarchivist
  [Pandas] Write data to Excel with dot decimals manonB 1 5,774 May-05-2021, 05:28 PM
Last Post: ibreeden
  Merging sorted dataframes using Pandas Robotguy 1 2,165 Aug-12-2020, 07:11 PM
Last Post: jefsummers
  Python Pandas for loop/while loop question mrashy 1 3,833 Mar-24-2020, 04:39 AM
Last Post: deanhystad
  Loop pandas data frame by position ? Johnse 1 2,241 Sep-06-2019, 12:26 AM
Last Post: scidam
  Need Help With Filtering Data For Excel Files Using Pandas eddywinch82 9 5,984 Aug-06-2019, 03:44 PM
Last Post: eddywinch82
  How to use the excel filename as a value to populate new column, using Pandas? Dequanharrison 5 6,417 Jun-26-2019, 11:11 PM
Last Post: Dequanharrison
  pandas writing to excel .. help anna 0 2,072 Jun-20-2019, 06:34 AM
Last Post: anna
  Python pandas remove default format in excel indra 0 2,655 Feb-06-2019, 04:48 AM
Last Post: indra
  Python read Password protected excel and convert to Pandas DataFrame FORTITUDE 2 17,015 Aug-30-2018, 01:08 PM
Last Post: FORTITUDE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020