Assistance on automated image/text Document - python

Hello all
I work for an estate agents and I am looking for a way of easily creating window cards for the properties (similar to this: http://www.inhabit.com.au/scr/windowcard1.png)
I am interested in creating a program that takes the text and images of the property and places them in the correct positioning (so a lamen can use it). It would then need to be printed.
I am fimiliar with graphic software such as Inkscape to create a template but I won't be the only one using it.
Would anybody have any idea of where to start creating an automated program that would create a window card?
Much oblidged
David

You can use weasyprint and python to create a such software.
You create first the HTML of the document, this is a template it won't move, so you only have to do it once, here is a basic template:
<html>
<body>
<h1>Trademark</h1>
<div id="container">
<div id="photos">
<img src="photo.jpg">
</div>
<div id="text">
{{ text }}
</div>
</div>
</body>
</html>
This script will write a file called input.html with the the {{ text }} area replaced by content of the text file. Mind the fact that you can add support for bold, italic even tables using markdown.
with open('card.html') as f:
card = f.read()
with open('text.txt') as f:
text = f.read()
with open('input.html', 'w') as f:
f.write(card.replace('{{ text }}', text))
Once you have a photo.jpg and the text you can execute the above script followed by the following command:
weasyprint -f png -s styles.css input.html output.png
This will generate something similar to:
This requires better design, but you get the idea.

Related

Embedded HTML code in Python string doesn't appear properly in Django

I'm facing a problem when I embed HTML code in my python's view.py
Basically, my objective is to customize the color of certain words only (based on the input). I want to do it by modifying the view.py
For example (my view.py):
def home(request):
form = request.POST.get('uncorrected')
texts = str(form) + '<span style="color: red">test text but this section is red</span>'
return render(request, 'corrector/home.html', {'text': texts})
Inside my index.html:
<textarea type="text" id="textarea" name="uncorrected">
{{ text }}
</textarea>
However, when I type "my text" in the textarea it displays only:
my text <span style="color: red">test text but this section is red</span>
It doesn't make the text red, it directly displays the code.
How can I make it work?
Django automatically escapes HTML to prevent XSS attacks. In order to render HTML as HTML, you just pipe in safe.
{{ text|safe }}
Django will still escape the HTML, but it will render it as well

Create hyperlink and give it an appealing name in Python

I am trying to rename my hyperlink to place in a pdf file. Thus, I do not want to give to the user a massive long link.
Let's say my link is like:
https://www.google.com/search?q=images+of+dogs&rlz=1C1OKWM_esES969ES969&sxsrf=AOaemvJFDb3FKdXO1Yqb3A1BdjWNfw0Edg:1632237403618&tbm=isch&source=iu&ictx=1&fir=D5X9VdSPli-xYM%252CHUMB4Zy1hHwFaM%252C_&vet=1&usg=AI4_-kShuarwW69ikZrP2YUHRVOpRHKKfQ&sa=X&ved=2ahUKEwiPs4aVrpDzAhUR1RoKHQiNAZIQ9QF6BAgPEAE&biw=2133&bih=1013&dpr=0.9#imgrc=D5X9VdSPli-xYM
And I want it to appear in the pdf like:
"Link to picture"
My code:
texto_body=f"Hi,<br> <br> This is a test with a link {link} <br> <br> Thanks,"
body=f"""\
<html>
<body>
<p style="color:black;"> {texto_body}</p>
<img src="cid:image1" alt="Logo" style="width:90px;height:90px;"><br>
</body>
</html>
"""
Solved. I found that text of the link
is the way to set up hyperlinks with a given name

Using Markdown2 with Django

I have never asked a question here before, please bear with me. I am working on a wiki project that has a requirement to convert markdown files using markdown2.
return render(request, "encyclopedia/entry.html", {
"content": markdown2.markdown(util.get_entry(title)), "title": title
})
Above is how I pass it to the HTML page and it renders on the page with the proper HTML tags, but it doesn't seem to use them. Below is how it appears on the browser.
<pre><code> # HTML
</code></pre>
<p>HTML is a markup language that can be used to define the structure of a web page. HTML elements include</p>
<ul>
<li>headings</li>
<li>paragraphs</li>
<li>lists</li>
<li>links</li>
<li>and more!
most recent major version of HTML is HTML5.</li>
</ul>
I am passing it directly to a Django template with the safe filter included as shown below.
<textarea name="content" rows="5" cols="50" readonly>
{{ content|safe }}
</textarea><br>
Thank you ahead of time, I hope I provided enough information to make my problem clear.
it looks like your content is going inside a <textarea> form field...that's going to prevent the browser from interpreting the HTML and just show exactly what is passed over.
Change to a <div> or something and it should work.

HTML: set id for onclick from list

Apologies if this question has already been asked. I'm new to HTML and I'm not familiar with the words I should use to find help with.
I'm using Flask and HTML to make a website.
My code:
<!DOCTYPE html>
<html>
<body>
<h3>Click the result you want to investigate</h3>
{% for r in results %}
<p id=r["links"] onclick="myFunction(id)"> {{r["title"]}} ({{r["address_snippet"]}}) </p>
{% endfor %}
<script>
function myFunction(id) {
document.getElementById(id).innerHTML = "CLICKED HERE";
}
</script>
</body>
</html>
I'm trying to print out a list of names, and etc to the screen. The user will click one name, this name will then be returned to a Python function for further analysis.
As a first attempt, I just want to change the text from the name to "CLICKED HERE". However, regardless of which name I click, only the first entry changes.
I can't figure out how to set the id from the container. Any help would be appreciated!
You need to update your code like this, so you get the actual value from the variable r["links"], by adding the brackets {{r["links"]}}
<p id="{{r["links"]}}" onclick="myFunction('{{r["links"]}}')"

pisa to generate a table of content via html convert

Does anyone have any idea how to use the tag so the table of content comes onto the 1st page and all text is coming behind. This is what i've got so far, it generates the table of content behind my text...
pdf.html
<htmL>
<body>
<div>
<pdf:toc />
</div>
<pdf:nextpage>
<br/>
<h1> test </h1>
<h2> second </h2>
some text
<h1> test_two </h1>
<h2> second </h2>
some text
</body>
</html>
I can't seem to get everything in the right position, even with the it doesn't seem to work... any help or documentation somewhere? The PISA docs are rly crappy with details actually...
Btw 1 more extra thing, is it possible to make this table of content jump to the right page? If yes how does this works?
Regards,
I found I couldn't get that pagebreak to work for me, so I used inline CSS and, specifically, the page-break property to fix it.
In your case, this should do the trick:
<div style="page-break-after:always;>
<pdf:toc />
</div>
<h1> test </h1> ...etc...
As far as the links are concerned, there may be a way to automatically generate them, but I found it easier to manually create a table of contents using links and anchors:
<h1>Table of Contents</h1>
<ul>
<li><a href="section1">The name of section 1</li>
<li><a href="section2">The name of section 2</li>
</ul>
<h2>The name of section 1</h2>
<a name="section1"></a>
<h2>The name of section 2</h2>
<a name="section2"></a>
There's obviously some duplication, but I haven't found it difficult to maintain for my documents. It depends how long or complicated you expect yours to became.
The bigger downside is that this option won't include page numbers.
Steve's comment about the page-break property is correct. I personally used a separate CSS file with
h2 {
page-break-before:always;
}
so that all of my sections would start on a new page.

Categories

Resources