QTooltip with "structured" html - python

I have a PyQt6 application that features a custom text editor.
When user hovers some word in this editor, a custom QToolTip is displayed.
I would have liked to make it fancier than the default one, with the following structure:
******* TITLE
* *
* IMG * - some text
* * - some other text
*******
I'm really noobish when it comes to HTML. I tried some code using <div> and <p> blocks, it delivered what I wanted when loading it in a navigator, but the result was not as expected in the application.
From what it seems, despite the documentation stating that Qt supports HTML blocks, what I want to achieve might be impossible.
Do you guys have any clue on what I could do to make it work? Above is an example of what I tried.
<div style="background-color: #2F3135;font-family: Franklin Gothic;font-size: 12;">
<div style="float: left;background-color: #2F3135;padding: 30px 20px 30px 30px;"><img src=MY_IMAGE width="64" height="64"/>
</div>
<p style="color: #FFFFFF;line-height:135%"><b><span style="background-color: #009900">TITLE:</b></span><br>
<span style="background-color: #009900;">some text<br></span>
<span style="background-color: #009900;">some other text<br></span>
</p>
</div>
EDIT
Following the answer from musicamante, I tried to do a table rather than using div blocks.
As I say in my answer to him, it works except if the title word of the second row is too long. Above should be a reproducable QTooltip example:
<table style="background-color: #454850;">
<tr>
<th rowspan=7 style="vertical-align: middle;padding-left: 20px;padding-right: 15px"><img src=MYIMAGE width="64" height="64"/></th>
<th><b><span style="color: #CECED7;font-family: Verdana, sans-serif;font-size: 10;">MAIN_TITLE</span></b></th>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td><b><span style="background-color: #307D30;color: #BACABA;font-family: Verdana, sans-serif;font-size: 10;">Inputs:</b></span></td>
</tr>
<tr>
<td><span style="background-color: #913131;color: #D0BFBF;font-family: Verdana, sans-serif;font-size: 10;">blabla</span></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td><b><span style="background-color: #913131;color: #D0BFBF;font-family: Verdana, sans-serif;font-size: 10;">Outputs:</b></td>
</tr>
<tr>
<td><span style="background-color: #913131;color: #D0BFBF;font-family: Verdana, sans-serif;font-size: 10;">blabla</span></td>
</tr>
</table>
So if MAIN_TITLE > 32 characters in my case, the 2nd column will not display properly in the QToolTip.
Maybe I messed up in the HTML code (I'm very new to HTML, never really worked with it).
Any tips is welcomed!

Related

How to use border-radius while converting html to pdf using xhtmltopdf

I am trying to round the corners of my table, border-radius doen't seem to work when I convert the below HTML to PDF using xhtmltopdf pdf generator. Below is the HTML written for content file name is sticker_print.html :
<div class="sticker" style="height:196px">
<table class="sticker_box" align="left">
<tr>
<td style="border: 1px solid #222;background-color: #ffffff;">
<h3 style="border-bottom: 1px solid #222222;">Batch Sticker</h3>
<h5 style="padding: 0 0 0 10px;">Batch ID</h5>
<p>MFG Date</p>
<p style="padding-bottom:0px;"><img src="http://www.computalabel.com/Images/C128ff#2x.png" width="195px" height="26px"><span> Bar Code </span></p>
<p style="text-align: left; padding-bottom: 0px;">
<img src="https://www.kaspersky.com/content/en-global/images/repository/isc/2020/9910/a-guide-to-qr-codes-and-how-to-scan-qr-codes-2.png" width="65px" height="65px">
<span style="display: block;margin-top: 0px;">QR Code</span>
</p>
</td>
</tr>
</table>
</div>
PDF CODE
pdf = render_to_pdf('sticker_print.html')
return HttpResponse(pdf, content_type='application/pdf')
Even though I'm not using the same PDF engine as you (and your question is 6 months old), I solved this issue by using corner-radius instead of border-radius on a table cell or div.

extract log file data and input directly into xhtml body

I've currently got a python script where a log file is put through and any defined 'excluded' keywords are stripped in the same file. I am attempting to then, after extracting the required words, input this into a pre-built XHTML file directly into the "body" section.
Is there a way that this can be accomplished?
My code for the writing from the extracted log file to the XHTML file is as follows, but this overwrites the XHTML file currently (which I expect as this is where I am stuck).
I have read up on BeautifulSoup but I don't want to go down that path, I want to strictly keep this all executed within the python file (if possible).
contents = open('\path\to\file.log','r')
with open("output.html", "w") as writehtml:
for lines in contents.readlines():
writehtml.write("<pre>" + lines + "</pre> <br>\n")
The formatting I have for my XHTML page within the section is as follows:
<body>
<tr>
<td bgcolor="#ffffff" style="padding: 40px 30px 40px 30px;">
<table border="1" cellpadding="0" cellspacing="0" width="100%%">
<tr>
<td style="padding: 10px 0 10px 0; font-family: Calibri, sans-serif; font-size: 16px;">
<!-- Body text from file goes here-->
Body Text Replaces Here
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</body>
Thanks.
How is this?
# You can read the template data and spell it in
contents = open('\path\to\file.log','r')
# Suppose that the beginning of your template is stored in this file,\path\template\start.txt
start = '''
<body>
<tr>
<td bgcolor="#ffffff" style="padding: 40px 30px 40px 30px;">
<table border="1" cellpadding="0" cellspacing="0" width="100%%">
<tr>
<td style="padding: 10px 0 10px 0; font-family: Calibri, sans-serif; font-size: 16px;">
'''
# start = open('\path\template\start.txt','r')
# Assume that the end of your template is in this file,\path\template\end.txt
end = '''
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</body>
'''
# end = open('\path\template\end.txt','r')
with open("output.html", "a") as writehtml:
writehtml.write(start)
for lines in contents.readlines():
writehtml.write("<pre>" + lines + "</pre> <br>\n")
writehtml.write(end)

can't get text from SPAN tag

The structure of the website I'm trying to parse looks like this:
<table border="0" cellpadding="3" cellspacing="0" width="100%">
<tr height="25">
<td class="th" style="border:none" width="2%"> </td>
<td class="th">movie</td>
<td class="th"> </td>
<td class="th"> </td>
</tr>
<tr id="place_1">
<td style="color: #555; vertical-align: top; padding: 6px">
<a name="1"></a>1.
</td>
<td style="height: 27px; vertical-align: middle; padding: 6px 30px 6px 0">
<a class="all" href="/326/">MOVIE TITLE IN SPANISH</a>
<br/>
<span class="text-grey">MOVIE TITLE IN ENGLISH</span>
</td>
<td style="width: 85px">
<div style="width: 85px; position: relative">
<a class="continue" href="/326/votes/">
9.191
</a>
<span style="color: #777">
(592 184)
</span>
</div>
</td>
</tr>
...
...
...
The problem is I can't get the text inside span-tag. I've tried .text as for a-tag, also tried .get_text(). But none of these worked. My code on Python:
for row in table.find_all('tr')[1:]:
info = row.find_all('td')
movies.append({
'spn_title' : info[1].a.text,
'eng_title' : info[1].span.text,
})
The errors I get:
AttributeError: 'NoneType' object has no attribute 'get_text'
or
'eng_title' : info[1].span.text AttributeError: 'NoneType' object has
no attribute 'text'
Try the following. Also, check your soup variable because I can run your code without problem. I suspect that somewhere later in the HTML you don't have one of these present in a row.
If the class names are consistent you could filter only qualifying rows having the appropriate type elements with those classes.Using bs4 4.7.1.
for row in table.select('tr :has(span.text-grey):has(a.all)'):
movies.append({
'spn_title' : row.select_one('.all').text,
'eng_title' : row.select_one('.text-grey').text
})
print(movies)
Otherwise, you want a way to handle if not present. For example,
for row in table.find_all('tr')[1:]:
movies.append({
'spn_title' : row.select_one('.all').text if row.select_one('.all') is not None else 'None',
'eng_title' : row.select_one('.text-grey').text if row.select_one('.text-grey') is not None else 'None'
})
print(movies)
I think that you should use innerHTML.
info[1].getElementsByTagName('span')[0].innerHTML
should work.
I have the same issue but I was able to resolve it.
example
<span class="a-offscreen">$10.99</span>
instead of Elem.FindElementByCss("span.a-offscreen").Text
use:
Elem.FindElementByCss("span.a-offscreen").FindElementByXPath("parent::*").Text
The trick is to get the text of the parent.
Btw, I am using VBA so you need to change it to Python Syntax.

How to select the option File from menu as per the html provided?

I'm trying to do automation and struck in the middle.
Cannot able to select option from a submenu.
Tried every solution from stack overflow and anything doesn't work.
Attaching the code.
<input id="arid_WIN_0_2000053" class="text " readonly="" style="top: 0px; left: 0px; width: 72px; height: 21px;" title="Screen" type="text">
This is the id i need to click so a drop down appears.
That is from differant section and the code is,
<table class="MenuTable" style="width: 93px;" cellspacing="0" cellpadding="0">
<tbody class="MenuTableBody">
<tr class="MenuTableRow">
<td class="MenuEntryName" nowrap="">Screen</td>
<td class="MenuEntryNoSub" arvalue="Screen"></td>
</tr>
<tr class="MenuTableRow">
<td class="MenuEntryName" nowrap="">File</td>
<td class="MenuEntryNoSub" arvalue="File"></td>
</tr>
<tr class="MenuTableRow">
<td class="MenuEntryName" nowrap="">Printer</td>
<td class="MenuEntryNoSub" arvalue="Printer"></td>
</tr>
<tr class="MenuTableRow">
<td class="MenuEntryNameHover" nowrap="">(clear)</td>
<td class="MenuEntryNoSubHover" arvalue=""></td>
</tr>
</tbody>
</table>
Once i selected the ID arid_WIN_0_2000053, i need to select option as File.
Thanks in advance.
As per the HTML to select an option e.g. File from the submenu you can use either of the following solutions:
driver.find_element_by_xpath("//input[#class='text' and #title='Screen'][starts-with(#id,'arid_WIN_0_')]").click()
driver.find_element_by_xpath("//table[#class='MenuTable']//tr[#class='MenuTableRow']//td[#class='MenuEntryName' and contains(.,'File')]").click()
Or
driver.find_element_by_xpath("//input[#class='text' and #title='Screen'][starts-with(#id,'arid_WIN_0_')]").click()
driver.find_element_by_xpath("//table[#class='MenuTable']//tr[#class='MenuTableRow']//td[#class='MenuEntryNoSub' and #arvalue='File']").click()
Use as Css locator : .MenuTableRow:nth-of-type(2) .MenuEntryName

How to write html text into text-area in robot framework?

I want to add the following code into a textarea input, how should I do this using Robot Framework?
<table style="margin-top: 1em; margin-left:1em;">
<tbody>
<tr>
<td><img src="/static/31bc33ac/images/48x48/orange-square.png" style="width: 48px; height: 48px; " class="icon-orange-square icon-xlg"></td>
<td style="vertical-align:middle">
<p><span>Started by user Vaishali Katkar</span></p>
</td>
</tr>
<tr>
<td><img src="/static/31bc33ac/plugin/metrics/images/48x48/clock.png" alt="" style="width: 48px; height: 48px; margin-right:1em;"></td>
<td style="vertical-align:middle">
<p>This run spent 1 sec waiting in the queue.</p>
</td>
</tr>
<tr>
<td><img src="/static/31bc33ac/plugin/git/icons/git-48x48.png" alt="" style="width: 48px; height: 48px; margin-right:1em;"></td>
<td style="vertical-align:middle"><b>Revision</b>: 861b6af99e7899d0b725459417e9fadfa25e2706
<ul>
<li>develop</li>
</ul>
</td>
</tr>
</tbody>
</table>
The HTML string contains many characters that should normally be escaped. For this reason it's easier to load it from a regular text file into a variable and then output that variable into the desired textarea element.
In the below example this being done using an example site:
FillInForm.robot
*** Settings ***
Library OperatingSystem
Library Selenium2Library
Test Setup Start Browser
Test Teardown Close Browser
Suite Teardown Close All Browsers
*** Test Cases ***
Fill Textarea with HTML code
${file_contents} Get File ./htmlcode.txt
Input Text name=longtext ${file_contents}
Sleep 3s
*** Keywords ***
Start Browser
Open Browser http://www.echoecho.com/htmlforms08.htm Chrome
htmlcode.txt
Contains the HTML code you want to insert.

Categories

Resources