html text area default value messing with page - python

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

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

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

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 add header and footer to a PDF file by rendering HTML page

I have generated a pdf file by rendering an HTML page. Here is what i did for that
return render_to_pdf(
html,
)
def render_to_pdf(html):
result = StringIO.StringIO()
pdf = pisa.pisaDocument(StringIO.StringIO(html.encode('utf-8') ),
dest=result)
if not pdf.err:
return HttpResponse(result.getvalue(),
content_type='application/pdf')
So this will give me a pdf file against the inputted HTML file.
I want to add header and footer for this pdf file , is there any way that i can do this dynamically. Dynamically in the sense for the first page in the pdf i need a footer and in the second page i need another footer that will be different from the first one, and in other page may have different footer so and so. So is there any way that i can include such feature in this, by specifying the page number. Any help would be greatly appreciated.

Uploading file using python mechanize

I am on a windows 10 laptop.
When i manually open submit.html on my local computer click and browse to namo.jpg namo.png and then submit, i get the website processing my image and return with result file within 15 seconds.
But I can't seem to get it to do the same using Python mechanize, when it run the script, the mechanize_results.html file keeps returning too quickly and telling me in their page that "Uploaded file is not a valid image. Only JPG, PNG and GIF files are allowed.. "
Not sure what i have to change to get the site to recognize my file submitted by my python mechanize script as an image file.
my submit.html file has this
<form name="myform" id="myform" action="http://deepdreamgenerator.com/upload-im" enctype="multipart/form-data" method="POST" id="upload-form">
<input type="hidden" name="_token" value="pfC1a6HGVdbWO7mCmKVkqVinCkSYOKkQxXZV9NY1">
<input type="file" name="file" id="upload"/>
<input type="submit" />
</form>
my python mechanize script has this
import mechanize
filename = 'C:/Users/tintran/Desktop/namo.png'
url = "file:///C:/Users/tintran/Desktop/submit.html"
br = mechanize.Browser()
br.set_handle_robots(False) # ignore robots
br.open(url)
br.select_form('myform')
br.set_all_readonly(False)
br.form.add_file(open(filename,'r'))
res = br.submit()
content = res.read()
with open("mechanize_results.html", "w") as f:
f.write(content)
https://docs.python.org/2/library/functions.html#open
If mode is omitted, it defaults to 'r'. The default is to use text mode, which may convert '\n' characters to a platform-specific representation on writing and back on reading. Thus, when opening a binary file, you should append 'b' to the mode value to open the file in binary mode, which will improve portability. (Appending 'b' is useful even on systems that don’t treat binary and text files differently, where it serves as documentation.) See below for more possible values of mode.
It's all about Windows here. So just use 'rb' for opening PNG file.

Categories

Resources