Home > Software engineering >  How can I remove the paragraphs of the text?
How can I remove the paragraphs of the text?

Time:06-23

I have succesfully created a program that send multiple emails, but I'm not having success to format the text correctly.

Every time that I try to create a new line, python automatically inserts a new paragraph and I don't want that. I have tried to place .lstrip at the ent of the text block, after the """ but didn't work.

The text should be like this:

"Dear owner,
 Our records indicate that your payment  have not been received so far. 
 The total due is:"

But is being printed on the email like this:

"Dear owner
    Our records indicate that your payment  have not been received so far. 
    The total due is:"

Please see my code below, thanks for any inputs.

import email
import win32com.client as client
import pandas as pd

tabela = pd.read_excel('over60.xlsx')
#print(tabela)

data = tabela[['Customer','Email','Owner','Total Due']].values.tolist()

for dado in data:

    owner_number = dado[0]
    to = dado[1]
    owner = dado[2]
    amount = dado[3]

    outlook = client.Dispatch('Outlook.Application')
    account = outlook.session.Accounts['myemail.com']

    email = outlook.CreateItem(0)
    email.To = 'myemail.com'
    email.Subject = f'Past Due Notice - {owner} - {owner_number}'
    email.Body =f""" Dear {owner}, 
    
    Our records indicate that your payment  have not been received so far. 
    
    The total due is ${amount: .2f}.

CodePudding user response:

The multiline string is including the indent at the start of the line, try unindenting the start of the line all the way to the left of the screen, or replacing newlines with \n e.g:

f"""Dear {owner},\n\nOur records indicate that your payment  have not been received so far.\n\nThe total due is ${amount: .2f}.

\n renders as a newline and not the literal characters "\n"

CodePudding user response:

Using """ preserves whitespace so a string like this...

"""Dear {owner}, 
    
    Our records indicate that your payment  have not been received so far. 
    
    The total due is ${amount: .2f}."""

Actually puts those leading spaces in.

If you want to keep using a multi-line string, you can apply something like inspect.cleandoc to clean up the string, like this...

import inspect

# ... your code ...

body = f""" Dear {owner}, 
    
    Our records indicate that your payment  have not been received so far. 
    
    The total due is ${amount: .2f}."""

email.Body = inspect.cleandoc(body)
  • Related