0

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!")
halfer
  • 19,824
  • 17
  • 99
  • 186
Deven Jain
  • 27
  • 1
  • 8
  • 1
    Have you tried using `.replace("\n", "\r\n")` or `.replace("\n", "\r")`? I don't think the problem is with `tkinter`. – TheLizzard May 19 '21 at 17:16
  • What does ```\r``` do? – Deven Jain May 19 '21 at 17:54
  • 1
    On windows `\r\n` is used for new lines. If you want to know more about it, read [this](https://stackoverflow.com/questions/15433188/r-n-r-and-n-what-is-the-difference-between-them) – TheLizzard May 19 '21 at 17:55
  • Problem resolved by using code: ```.replace("\n","\r")```. Thank you so much dude, I cannot express my gratitude to you using words, Please Write it as an answer and I will approve it as a **valid solution** and upvote. Thank you once again bro, I really appreciate it! – Deven Jain May 19 '21 at 18:05

1 Answers1

1

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.

TheLizzard
  • 7,248
  • 2
  • 11
  • 31
  • Thank you so much sir, I really appreciate it! :) I completed this GUI Application which I was building however, once I made it into an exe file to send to the person for whom it is intended, An error message comes on screen. ```Fatal Error Detected``` **Failed to execute script** ```mainV2``` – Deven Jain May 19 '21 at 18:26
  • 1
    @DevenJain How do you compile it? Are you compiling it as a 32 or 64 bit program? What OS are you using? What OS are they using? I don't know much about compiling python programs so I would advise you to ask a new question. – TheLizzard May 19 '21 at 19:06