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
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.


treeof your project.print()(andprint(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.