Python , Changing a font size of a string variable

2019-09-26 07:58发布

问题:

I have a variable that gets sent to a email as text but the text is all pretty much a standard size with everything the same. I would like to add some emphasis to it as well as make it bigger and make it bold if possible. Here is the code I would like to edit.

  final_name = "Changes by" + str(name)+" ***"

I know it isn't much but I would like to know if it is possible if I can make a variable string bold and with a slightly bigger font size.

回答1:

Strings don't have a font size. Strings store sequences of characters (unicode strings) or bytes to be interpreted as characters (byte strings).

Font size is an element of presentation, and is a function of whatever presentation and rendering system you are using.

As you mention an email, you could create a multipart email with an HTML part, and format it accordingly in that HTML document.



回答2:

If python is sending out the email through your SMTP server; You'll want to change the email type to html formatting by setting the content-type to text/html

# Build the email message
sender_name = "My script"
sender_email = "someEmail@company.com"
reciver_emails = ['receive1@company.com', 'receive2@company.com']
subject = "MY email subject"
message = "HTML <b>bolded</b> text"

email = ("From: %s <%s>\r\n"
         "To: %s\r\n" % (sender_name, sender_email, receiver_emails))

email = email + "CC: %s\r\n" % (cc_emails)
email = email + ("MIME-Version: 1.0\r\n"
                 "Content-type: text/html\r\n"
                 "Subject: %s\r\n\r\n"""
                 "<html>\r\n"
                 "%s\r\n"
                 "</html>" %  (subject, message))

You can then add html type tags as the some of the answers have stated.