pythoncsvimg2pdf

Python Convert List Image to multiple PDF


In the case

I have a folder of image list and list name file with csv

I want to write a python script to convert list of PNG's to multiple pdf file with name file of pdf from csv file at column D

like this: enter image description here

Then i have tried python code like this:

import os
import csv
import img2pdf
from PIL import Image

# Set this to the folder with your files
folder_path = r"C:\Users\dede\Desktop\product_report"
# Set this to the name of your CSV
csv_file = "product.csv"

with open(os.path.join(folder_path, csv_file), newline='') as file:
    reader = csv.reader(file, delimiter='|')
    next(reader) # Skip header row
    for i, row in enumerate(reader, start=1):
        old_name = f"{i}.png"        
        new_name = row[0] + ".pdf"
        old_path = os.path.join(folder_path, old_name)
        new_path = os.path.join(folder_path, new_name)
        if os.path.exists(old_path):
            # os.rename(old_path, new_path)
            # print(f"Renamed {old_name} to {new_name}")
            image = Image.open(old_name)
            pdf_bytes = img2pdf.convert(image.old_name)
            file = open(new_name, "wb")
            file.write(pdf_bytes)
        else:
            print(f"File {old_name} not found")

This code was error result

Is there a better way to do this, Somebody help me. Thank you.


Solution

  • You have not provided a sample of your CSV or details of your other constraints but simply hints. Thus, you may need to adapt my suggestions.

    There are far faster methods to use a one line command for PNG to PDF utility such as MuPDF (by far the fastest). However as this is a Python Question, we can do similar with PyMuPDF.

    Your hints show a character seperated output should look like

    Uncombined Data|||Combined Data
    Item|Size||Item / Size
    Black Pants|Small||Black Pants Small
    Black Pants|Medium||Black Pants Medium
    Black Pants|Large||Black Pants Large
    Tan Pants|Small||Tan Pants Small
    ....
    

    And others have suggested you should be using print debugging to watch progress.

    Progress CSV row 3 convert from 1.png to Black Pants Small.pdf
    Progress CSV row 4 convert from 2.png to Black Pants Medium.pdf
    Progress CSV row 5 convert from 3.png to Black Pants Large.pdf
    Progress CSV row 6 convert from 4.png to Tan Pants Small.pdf
    Skipped row 6: no such file: 'C:\Users...\WPy32-310111\product_report\4.png'
    Press any key to continue . . .
    

    The following code is adapted from the PyMuPDF documentation, and there are shorter methods but shows a principal methodology. MuPDF can open various image types as documents then save the page as a PDF very simply.

    import csv, os, pymupdf
    
    # Set this to the folder with your files
    folder_path = r"C:\Users\...\product_report"
    # Set this to the name of your CSV
    csv_file = "product.csv"
    
    with open(os.path.join(folder_path, csv_file), newline='') as file:
        reader = csv.reader(file, delimiter='|')
        next(reader); next(reader)  # Skip 2 header rows
    
        for i, row in enumerate(reader, start=3):                     # Adjusted to match lines start at 3
            try:
                new_name = row[3].strip()                             # row [3] is CSV column D
                img_path = os.path.join(folder_path, f"{i - 2}.png")  # Adjusted by -2 to match lines
                pdf_path = os.path.join(folder_path, f"{new_name}.pdf")
    
                print(f"Progress CSV row {i} convert from {i - 2}.png to {new_name}.pdf")
    
                png_doc = pymupdf.open(img_path)
                pdfbytes = png_doc.convert_to_pdf()                   # make a 1-page PDF image stream
                png_doc.close()                                       # optional good practice
    
                imgpdf = pymupdf.open("pdf", pdfbytes)                # insert the image to PDF
                imgpdf.save(pdf_path)
                imgpdf.close()                                        # optional good practice
    
            except Exception as e:
                print(f"Skipped row {i}: {e}")
    

    enter image description here