Search code examples
python-3.xcsvtkinterphotoshop-scripttkinter-text

How to fix unidentified character problem while passing data from TKinter to Photoshop via Python script?


I made a GUI Application which looks like this:

A picture of my GUI

The ones marked red are Tkinter Text widgets and the ones marked yellow are Tkinter Entry widgets

After taking user input, the data is to be added to a PSD file and then rendered as an image. But Lets say, after taking the following data as input:

Filled Data

It renders the following Photoshop file:

Generated Prescription

How do I fix this issue that it does not recognize "\n" properly and hence the rendered document is rendered useless.

Here is the code which deals with converting of the accepted user data into strings and then adding it to Photoshop template and then rendering it:

def DataAdder2CSV():
    global edate, eSNO, eage, egender, ename, ePID, econtact, ecomp, eallergy, ehistory, eR
    e=edate.get()
    a=eSNO.get()
    d=eage.get()
    f=egender.get()
    b=ename.get()
    c=ePID.get()
    g=econtact.get()
    h=ecomp.get(1.0,END)
    i=eallergy.get(1.0,END)
    j=ehistory.get(1.0,END)
    k=eR.get(1.0,END)
    data=[a,b,c,d,e,f,g,h,i,j,k]
       
    file=open("Patient_Data.csv","a", newline="")
    writer=csv.writer(file, delimiter=",")

    writer.writerow(data)

    file.close()
    messagebox.showinfo("Prescription Generator", "Data has been saved to the database successfully!")
    import win32com.client, os
    objShell = win32com.client.Dispatch("WScript.Shell")
    UserDocs = objShell.SpecialFolders("MyDocuments")
    from tkinter import filedialog

    ExpDir=filedialog.askdirectory(initialdir=UserDocs, title="Choose Destination Folder")

    psApp = win32com.client.Dispatch("Photoshop.Application")
    psApp.Open("D:\Coding\Python Scripts\Dr Nikhil Prescription App\Prescription Generator\Presc_Template.psd")
    doc = psApp.Application.ActiveDocument

    lf1 = doc.ArtLayers["name"]
    tol1 = lf1.TextItem
    tol1.contents = b

    lf2 = doc.ArtLayers["age"]
    tol2 = lf2.TextItem
    tol2.contents = d

    lf3 = doc.ArtLayers["gender"]
    tol3 = lf3.TextItem
    tol3.contents = f

    lf4 = doc.ArtLayers["pid"]
    tol4 = lf4.TextItem
    tol4.contents = c

    lf4 = doc.ArtLayers["date"]
    tol4 = lf4.TextItem
    tol4.contents = e

    lf5 = doc.ArtLayers["contact"]
    tol5 = lf5.TextItem
    tol5.contents = g

    lf6 = doc.ArtLayers["complaint"]
    tol6 = lf6.TextItem
    varH="                       "+h.rstrip("\n")
    tol6.contents =varH

    lf7 = doc.ArtLayers["allergy"]
    tol7 = lf7.TextItem
    tol7.contents = i.rstrip("\n")

    lf8 = doc.ArtLayers["history"]
    tol8 = lf8.TextItem
    varJ="                                              "+j.rstrip("\n")
    tol8.contents =varJ

    lf9 = doc.ArtLayers["R"]
    tol9 = lf9.TextItem
    tol9.contents = k.rstrip("\n")
    options = win32com.client.Dispatch('Photoshop.ExportOptionsSaveForWeb')
    options.Format = 13
    options.PNG8 = False
    pngfile =ExpDir+f"/{c}-{b}_({e}).png"
    doc.Export(ExportIn=pngfile, ExportAs=2, Options=options)
    messagebox.showinfo("Prescription Generator", "Prescription has been saved in the desired location successfully!")

Solution

  • There are 3 ways of expressing new line characters:

    • MacOS uses \r
    • Linux uses \n
    • Windows uses \r\n

    Python and tkinter use \n but it looks like psApp.Application uses \r instead. That is why the document isn't rendered properly. For more info read the answers to this question.