0

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.

6
  • 2
    What error? Add the error message including the stack trace if any. Commented Sep 10 at 17:30
  • Please provide more details: Title and type of error, full text error message including file names, your environments like OS , python version, package version and maybe the tree of your project. Commented Sep 10 at 18:33
  • 1
    always put full error message (traceback) because there are other useful information. Commented Sep 10 at 20:11
  • 1
    whithout error message we have no idea what is the problem. Don't expect that we will run code to see problem. Besides code may works correctly on our computers. And we can't read in your mind. You have to show all details in question (not in comments) as text (not image) Commented Sep 10 at 20:13
  • 2
    Maybe first use print() (and print(type(...)), print(len(...)), etc.) to see which part of code is executed and what you really have in variables. It is called "print debugging" and it helps to see what code is really doing. Commented Sep 10 at 20:14

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

it's work, thank you

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.