Skip to main content
Participant
July 21, 2024
Question

How to Import a CSV file as a dataset in photoshop using python

  • July 21, 2024
  • 0 replies
  • 1051 views

Im trying to write a piece of python code that can take all the contents CSV file and import it as a dataset, my code is pretty buggy and rough and my output is really long but its replacing all the text variables just fine, my problem is it wont change COVER as its value is a path to the image that Im trying to replace with. my CSV headers are TEST1 ,TEST2 ,TEST3 ,TEST4 ,COVER, and the layer names match them identically, if anyone is able to give me a hand that would be greatly appreciated ive been on this for like a month heres the code that I have

 

 

import comtypes.client
import pandas as pd

def open_PS(template_path😞
    try:
        ps_app = comtypes.client.GetActiveObject("Photoshop.Application")
    except OSError:
        ps_app = comtypes.client.CreateObject("Photoshop.Application")
   
    ps_doc = ps_app.Open(template_path)
    ps_app.Visible = True
   
    return ps_app, ps_doc

def find_layer_by_name(layers, name😞
    for layer in layers:
        try:
            if layer.Name == name:
                return layer
            if hasattr(layer, 'Layers'😞
                found_layer = find_layer_by_name(layer.Layers, name)
                if found_layer:
                    return found_layer
        except Exception as e:
            print(f"Error accessing layer '{layer.Name}': {e}")
    return None

def print_layers(layers, indent=0😞
    for layer in layers:
        try:
            print(' ' * indent + layer.Name)
            if hasattr(layer, 'Layers'😞
                print_layers(layer.Layers, indent + 4)
        except Exception as e:
            print(f"Error accessing layers of '{layer.Name}': {e}")

def import_csv_to_photoshop(ps_app, ps_doc, csv_path😞
    # Read the CSV file into a DataFrame
    data = pd.read_csv(csv_path)
   
    print("Layers in the document:")
    print_layers(ps_doc.Layers)

    processed_layers = set()
    failed_layers = set()

    for index, row in data.iterrows():
        for col in data.columns:
            try:
                # Find the layer by name
                layer = find_layer_by_name(ps_doc.Layers, col)
               
                if not layer:
                    print(f"Layer '{col}' not found in the document.")
                    failed_layers.add(col)
                    continue

                # Check if the layer is a text layer
                if hasattr(layer, 'TextItem'😞
                    if col in processed_layers:
                        print(f"Layer '{col}' already processed.")
                        continue
                   
                    # Update the text content of the layer
                    layer.TextItem.Contents = row[col]
                    processed_layers.add(col)
                else:
                    print(f"Layer '{col}' is not a text layer.")
                    failed_layers.add(col)
                   
            except Exception as e:
                print(f"Error processing column '{col}' with value '{row[col]}': {e}")
                failed_layers.add(col)

    # Save the document (optional)
    ps_doc.Save()
   
    # Print summary of processing
    print("\nProcessing Summary:")
    print(f"Processed Layers: {', '.join(processed_layers) if processed_layers else 'None'}")
    print(f"Failed Layers: {', '.join(failed_layers) if failed_layers else 'None'}")

# Example usage:
template_path = r"E:/Downloads/TEST/TEST.psd"  # Corrected path
csv_path = r"E:/Downloads/TEST/TEST.csv"      # Corrected path
ps_app, ps_doc = open_PS(template_path)
import_csv_to_photoshop(ps_app, ps_doc, csv_path)
This topic has been closed for replies.