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

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()

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

Create txt file template with django email

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,

How to write for loop content in html code python?

Below is the code
urls.append('http://google.com')
urls.append('http://stacoverflow.com')
whole = """<html>
<head>
<title>output -</title>
</head>
<body>Below are the list of URLS
%s // here I want to write both urls.
</body>
</html>"""
for x in urls:
print x
f = open('myfile.html', 'w')
f.write(whole)
f.close()
So this is the code for saving the file in HTML format. But I can't find the way to get the contents of for loop into HTML file. In other words, I want to write a list of indexes elements i.e. http://google.com, http://stackoverflow.com into my HTML file. As you can see that I have created myfile.html as HTML file, So I want to write both URLs which are in the list of indexes into my HTML file
Hope this time I better explain?
How can I? Would anyone like to suggest me something? It would be a really big help.
Try below code:
urls.append('http://google.com')
urls.append('http://stacoverflow.com')
whole = """<html>
<head>
<title>output -</title>
</head>
<body>Below are the list of URLS
%s
</body>
</html>"""
f = open('myfile.html', 'w')
f.write(whole % ", ".join(urls))
f.close()

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

Loading mako templates from files

I'm new to python and currently trying to use mako templating.
I want to be able to take an html file and add a template to it from another html file.
Let's say I got this index.html file:
<html>
<head>
<title>Hello</title>
</head>
<body>
<p>Hello, ${name}!</p>
</body>
</html>
and this name.html file:
world
(yes, it just has the word world inside).
I want the ${name} in index.html to be replaced with the content of the name.html file.
I've been able to do this without the name.html file, by stating in the render method what name is, using the following code:
#route(':filename')
def static_file(filename):
mylookup = TemplateLookup(directories=['html'])
mytemplate = mylookup.get_template('hello/index.html')
return mytemplate.render(name='world')
This is obviously not useful for larger pieces of text. Now all I want is to simply load the text from name.html, but haven't yet found a way to do this. What should I try?
return mytemplate.render(name=open(<path-to-file>).read())
Thanks for the replies.
The idea is to use the mako framework since it does things like cache and check if the file has been updated...
this code seems to eventually work:
#route(':filename')
def static_file(filename):
mylookup = TemplateLookup(directories=['.'])
mytemplate = mylookup.get_template('index.html')
temp = mylookup.get_template('name.html').render()
return mytemplate.render(name=temp)
Thanks again.
Did I understand you correctly that all you want is read the content from a file? If you want to read the complete content use something like this (Python >= 2.5):
from __future__ import with_statement
with open(my_file_name, 'r') as fp:
content = fp.read()
Note: The from __future__ line has to be the first line in your .py file (or right after the content encoding specification that can be placed in the first line)
Or the old approach:
fp = open(my_file_name, 'r')
try:
content = fp.read()
finally:
fp.close()
If your file contains non-ascii characters, you should also take a look at the codecs page :-)
Then, based on your example, the last section could look like this:
from __future__ import with_statement
#route(':filename')
def static_file(filename):
mylookup = TemplateLookup(directories=['html'])
mytemplate = mylookup.get_template('hello/index.html')
content = ''
with open('name.html', 'r') as fp:
content = fp.read()
return mytemplate.render(name=content)
You can find more details about the file object in the official documentation :-)
There is also a shortcut version:
content = open('name.html').read()
But I personally prefer the long version with the explicit closing :-)

Categories

Resources