Reputation: 35
I am having a really hard time getting the "marker" to show up. I do not know how to use .format()
correctly to show the marker inside the string.
Does the variable need to be in a specific location in the string? Trying to get a grasp of this for the first time. Sorry if I am asking basic questions.
Keep getting : """.format(marker) KeyError: 'font-family'
. Not sure where the problem is.
marker = "AUniqueMarker"
# Create the body of the message (a plain-text and an HTML version).
text = "This is a test message.\nText and html."
html = """\
<html xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:w="urn:schemas-microsoft-com:office:word"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:m="http://schemas.microsoft.com/office/2004/12/omml"
xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv=Content-Type content="text/html; charset=windows-1252">
<meta name=ProgId content=Word.Document>
<meta name=Generator content="Microsoft Word 15">
<meta name=Originator content="Microsoft Word 15">
<link rel=File-List href="Law_files/filelist.xml">
<!--[if gte mso 9]><xml>
# (...)
-->
</style>
<!--[if gte mso 10]>
<style>
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:"Table Normal";
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:"";
mso-padding-alt:0in 5.4pt 0in 5.4pt;
mso-para-margin:0in;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;}
</style>
<![endif]--><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026"/>
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1"/>
</o:shapelayout></xml><![endif]-->
</head>
<body lang=EN-US style='tab-interval:.5in'>
{marker}
</body>
</html>
""".format(marker=marker)
Upvotes: 2
Views: 4886
Reputation: 975
If you don't want to manipulate the HTML code and add extra {} or %, python's Template string might be exactly what you’re looking for. Let’s take a look at an example:
from string import Template
t = Template('<body> $marker </body>')
t.substitute(marker='Hello World!')
'<body> Hello World! </body>'
Upvotes: 0
Reputation:
You need to escape the presence of other curly braces ({
and }
) in the string, otherwise the string will be misinterpreted by format
.
You have to repeat the characters to escape them. In the string you are calling format
on, change the line
{mso-style-name:"Table Normal";
to
{{mso-style-name:"Table Normal";
and similarly for the closing bracket.
Upvotes: 5
Reputation: 26
You must double the {
and }
in your string, otherwise format
will try to interpret text between braces.
html = """\
your html code
""".replace("{", "{{").replace("}", "}}").format(marker=marker)
EDIT : replace
will transform {marker}
into {{marker}}
, so it will not be interpreted by format
...
Upvotes: 1