how to open html file automatically using webbrowser package in python - python

I have a python script that use pandas and create dataframe from csv file and i want to display the dataframe information using pandas-profiling package and show the report in the browser once the user run the function .
But the system does not open the browser and display this error:
ValueError: startfile: filepath too long for Windows
code:
def displayDfInfo(self,df):
profile = pp.ProfileReport(df)
html_profile = profile.to_html()
webbrowser.open(html_profile,new=1)
where is the error and how to fix it?

I would simplify it to this:
import webbrowser
html = "myhtml.html"
webbrowser.open(html)

Related

How to read console promp in pyWinauto

I'd like to control MobaXterm app with Python and trying to find a way for the code to read and fetch the string from the promp in each tab (terminal).
from pywinauto.application import Application
app = Application(backend='uia').start('"C:\\Program Files (x86)\\Mobatek\\MobaXterm\\MobaXterm.exe"').connect(title='val',timeout=100)
textEditor = app.val.child_window(control_type="Tab").wrapper_object()
print(textEditor.Edit.get_line(0))
textEditor.type_keys("^{TAB 2}")
textEditor.type_keys("test",with_spaces = True)
I tried to use get_line above, but it gives the following error:
print(textEditor.Edit.get_line(0))
AttributeError: 'TabControlWrapper' object has no attribute 'Edit'
Can you help me to find a way?

How can i view an html output file from a gmplot draw command inside of a Google Colab notebook?

I am trying to use colab and googlemaps to display a heatmap image overlayed onto google maps. I did some research and found 2 possible solutions at this link but both solutions are not working.
Display / Render an HTML file inside Jupyter Notebook on Google Colab platform
Maybe something has changed recently?
Here is my code:
pip install gmplot
import gmplot
gmap1 = gmplot.GoogleMapPlotter(30.3164945,
78.03219179999999, 13, apikey='AIzaSyBCyhpxDYIxIq9uINYxDK5aIjWSokUvsvY' )
gmap1.draw( "map11.html" )
import os
print(os.getcwd())
!ls
/content
map11.html sample_data
import IPython
IPython.display.HTML(filename='/content/map11.html')
nothing is displayed.
from IPython.display import IFrame
IFrame(src='/content/map11.html', width=900, height=600)
localhost refused to connect.
Your local browser cannot read a file from the remote VM filesystem. Instead of passing the HTML by filename, try displaying the HTML content directly:
IPython.display.HTML(open('/content/map11.html').read())

Python: How to open map created with folium in browser when running application

I have a very small application written in PyCharm using python3:
import folium
map = folium.Map(location=[58.1, 23.3], zoom_start=10)
map.save('map2.html')
This will create a map2.html which I can access in my browser by using pycharm and the url looks like: http://localhost:63342/iss-country/map2.html?_ijt=dcsefdg8om4ddfovlt5ooq6ro5
How can I automatically open this in my browser? So when I run the application it does not only generate the html page but also visits it immediatley. I found the webbrowser module which can be useful but how do I know the correct localhost url?
I don't see the issue with using the webbrowser module. Just make the file name and path a variable and call the webbrowser open method.
output_file = "map2.html"
map = folium.Map(location=[58.1, 23.3], zoom_start=10)
map.save(output_file)
webbrowser.open(output_file, new=2) # open in new tab

Trying to pass extracted text from Tesseract OCR to custom google search

Having some trouble with a project and hopefully someone can help! I'm trying to take extracted text from tesseract OCR and use that text as the search query of Google Chrome searches. My shell script can extract the text and launch Chrome, but I cant figure out how to send the text to the searchbar of chrome. Below are some pictures of my script. I'm extremely new to coding, so any help is appreciated.
Shell script
echo "Realtime Screen OCR"
while true
do
echo "Waiting for trigger"
read
screencapture -R31,205,420,420 screens.png
tesseract screens.png ocr
OCR=`cat ocr.txt`
python3 launch1.py $OCR
##/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome $OCR
echo "Opened Chrome...waiting for next question"
done
Python Script
import urllib.parse
search_query = input("enter search query")
query_encoded = urllib.parse.quote_plus(search_query)
google_search_url = "http://www.google.com/search?q=" +
format(query_encoded)
import webbrowser
webbrowser.open(google_search_url)
It appears to me that the only step you're missing is grabbing the text in your Python code and passing it into the search query. You can do this using sys. In this example we replace the user-inputted text with retrieving the argument from the command line, assuming that this is what you're trying to do.
import urllib.parse
import sys
search_query = sys.argv[1]
query_encoded = urllib.parse.quote_plus(search_query)
google_search_url = "http://www.google.com/search?q=" +
format(query_encoded)
import webbrowser
webbrowser.open(google_search_url)

Python configure pdfkit on windows

I started to learn python recently and I want to convert existing html file to pdf file. It is very strange, but pdfkit seems to be the only lib for pdf docs for python.
import pdfkit
pdfkit.from_file("C:\\Users\\user\Desktop\\table.html", "out.pdf")
An error occurs:
OSError: No wkhtmltopdf executable found: "b''"
How to configure this lib properly on windows to make it work? I can't get it :(
It looks like you need to install wkhtmltopdf. For windows, the installer can be found at https://wkhtmltopdf.org/downloads.html
Also check out a post by this guy, who is having the same problem: Can't create pdf using python PDFKIT Error : " No wkhtmltopdf executable found:"
I found working solution.
If you want to convert files to pdf format just don't use python for this purpose.
You need to include DOMPDF library into your php script on your local/remove server. Something like this:
<?php
// include autoloader
require_once 'vendor/autoload.php';
// reference the Dompdf namespace
use Dompdf\Dompdf;
if (isset($_POST['html']) && !empty($_POST['html'])) {
// instantiate and use the dompdf class
$dompdf = new Dompdf();
$dompdf->loadHtml($_POST['html']);
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser
$dompdf->stream();
} else {
exit();
}
Then in your python script you can post your html or whatever content to your server and get generated pdf file as a response. Something like this:
import requests
url = 'http://example.com/html2pdf.php'
html = '<h1>hello</h1>'
r = requests.post(url, data={'html': html}, stream=True)
f = open('converted.pdf', 'wb')
f.write(r.content)
f.close()

Categories