Create txt file template with django email - python

I have a question according to emails generated in my Django application. I created an HTML file, but I have question with my txt file and tags used.
In my HTML file:
links look like: My link
bold looks like: <b>My text</b>
variables look like: {{ my_variable }}
But in my txt file, HTML tags work ? How I can display links and bold text in my .txt file which will send by email ?
Thank you very much,

Related

Script needed to create directory listing from html breadcrumb file

I've got a "html" text file that has a file tree directory listed in bullet format. I need to recombine this into a normal directory string.
What I have:
Snippet of HTML Document
How it displays:
How the HTML Docuement Displays
What I need it to look like:
C:\computer list.csv
D:\DeptShare\Accounting.DS_Store
D:\DeptShare\Accounting\New Folder\8-1-18.xls
D:\DeptShare\Accounting\Physical Inventory\Copy of PhysInv Data Entry Dump 2016.xlsx
D:\DeptShare\Accounting\Physical Inventory\ PhysInv Data Entry Dump 2016.xlsx

how to write svg code to a file and save It in media folder in Django?

I have a function that generates a barcode as svg code.
I need to display this svg in an HTML PDF template. but unfortunately inline svg doesn't work.
so as a workaround I need to write this code into a file and save it in the media folder so I can access it by tag in the HTML PDF template.
Below is the function that generates the barcode:
def barcode_number(self):
number = self.get_real_instance().number
svg_code = generate('code39', str(number).zfill(20), pil=True)
code = svg_code.decode("utf-8")
## here I want to write code into barcode.svg file
safe_html = mark_safe(svg_code)
return safe_html
How can I write this code to a svg file and save it in '/media/uploads' ?
Thank you

Combining head, body and footer in HTML (or with CSS?)

I have two separate files called head.html and footer.html, each of which has several formatting options. In a python script, I am processing some text for the body:
sometext=inspect.cleandoc(f'''
<body>
This is some text.
</body>
''')
Html_file= open('path/to/my/output.html',"w")
Html_file.write(sometext)
Html_file.close()
How do I:
Include the head.html and footer.html in my output.html in python, with the body in the middle? I was thinking, perhaps I can open head.html > write to output.html, open this file with open( ... , 'a') > write the footer. But perhaps there is a better way?
I am a bit confused as to how to use the CSS together with the html and my text generated in python. I understand how to write each of these, but not really sure how to get them to work together.
My goal is to use the head.html, body, footer.html in a single html file, and then convert it to PDF using weasyprint.
This script should do what you describe:
# open output file
Html_file= open('path/to/my/output.html',"w")
# write from header file to output file
with open('path/to/my/header.html') as header_file:
for line in header_file:
Html_file.write(line)
# write body
sometext=inspect.cleandoc(f'''
<body>
This is some text.
</body>
''')
Html_file.write(sometext)
# write from footer file to output file
with open('path/to/my/footer.html') as footer_file:
for line in footer_file:
Html_file.write(line)
# close output file
Html_file.close()

Neatly printing a .txt file in python

I have a .txt file with text like the following:
Project Gutenberg Australia
a treasure-trove of literature
treasure found hidden with no evidence of ownership
but whenever I try to read this file in Python (using Flask - I'm uploading the file to a site), using the following lines:
if request.method == 'POST':
f = request.files['file']
f.save(secure_filename(f.filename))
f.stream.seek(0)
content = f.read()
return render_template("book.html", text=content)
My "book.html" file is like the following:
<pre>
{{ text }}
</pre>
I get something like the following:
b'\xef\xbb\xbf\r\nProject Gutenberg Australia\r\na treasure-trove of literature\r\ntreasure found hidden with no evidence of ownership\r\n\r\n\r\n\r\n\r\...]
How can I fix this so that what is displayed on my site is just like what is displayed in the .txt file? Do I need to modify "book.html" or just read the file differently with Python?
Thanks!
You just need to .decode() your bytes:
print(b'\xef\xbb\xbf\r\nProject Gutenberg Australia\r\na treasure-trove of literature\r\ntreasure found hidden with no evidence of ownership\r\n\r\n\r\n\r\n\r\...]'.decode())
gives:
Project Gutenberg Australia
a treasure-trove of literature
treasure found hidden with no evidence of ownership
\...]

html text area default value messing with page

I have a python cgi script that creates a text area and fills it with default value from the contents of a file. This used to work but recently I noticed that with change in content on the file ;the html is rendered incorrectly and the submit button and some parts of the file contents to be shown in the text area(as default content) etc is missing or messing up with the total page's html
print('<form action="x.cgi" method="post">')
print('<textarea name="textcontent" cols="120" rows="50">')
with open('somefile', 'r') as content_file:
content = content_file.read()
content_file.close()
print(content)
print('</textarea>')
print('<HR>')
print('<input type="submit" value="Submit" />')
print('</form>')
What can be done so that the contents of somefile doesnt mess with the html form . Note that somefile is a configuration file and I need everything in the file to be printed as such so user can make necessary change and submit it

Categories

Resources