Categories
Python Answers

How to show a file dialog in Python?

Sometimes, we want to show a file dialog in Python.

In this article, we’ll look at how to show a file dialog in Python.

How to show a file dialog in Python?

To show a file dialog in Python, we can call filedialog.askopenfilename.

For instance, we write

import tkinter as tk
from tkinter import filedialog

root = tk.Tk()
root.withdraw()

file_path = filedialog.askopenfilename()

to call filedialog.askopenfilename to open the file dialog.

And once a file is selected, its path will be returned as the value.

Conclusion

To show a file dialog in Python, we can call filedialog.askopenfilename.

Categories
Python Answers

How to JSON serialize sets with Python?

Sometimes, we want to JSON serialize sets with Python.

In this article, we’ll look at how to JSON serialize sets with Python.

How to JSON serialize sets with Python?

To JSON serialize sets with Python, we can use the json.dumps method with a custom json.JSONEncoder subclass.

For instance, we write

import json

class SetEncoder(json.JSONEncoder):
   def default(self, obj):
      if isinstance(obj, set):
         return list(obj)
      return json.JSONEncoder.default(self, obj)

s = json.dumps(set([1,2,3,4,5]), cls=SetEncoder)

to create the SetEncoder class which is subclass of the json.JSONEncoder class.

In it, we add the default method.

And in the default method, we return the set converted to a list if the obj object is a set.

And then we call json.JSONEncoder.default(self, obj) to serialize the obj object.

Next, we call json.dumps with a set and the cls argument set to SetEncoder to use SetEncoder to serialize the set into a JSON string.

Conclusion

To JSON serialize sets with Python, we can use the json.dumps method with a custom json.JSONEncoder subclass.

Categories
Python Answers

How to run Selenium Webdriver with a proxy in Python?

Sometimes, we want to run Selenium Webdriver with a proxy in Python.

In this article, we’ll look at how to run Selenium Webdriver with a proxy in Python.

How to run Selenium Webdriver with a proxy in Python?

To run Selenium Webdriver with a proxy in Python, we can use the Proxy class.

For instance, we write

from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy, ProxyType

prox = Proxy()
prox.proxy_type = ProxyType.MANUAL
prox.http_proxy = "ip_addr:port"
prox.socks_proxy = "ip_addr:port"
prox.ssl_proxy = "ip_addr:port"

capabilities = webdriver.DesiredCapabilities.CHROME
prox.add_to_capabilities(capabilities)

driver = webdriver.Chrome(desired_capabilities=capabilities)

to create a Proxy object.

Then we set the proxy settings for various protocols with

prox.proxy_type = ProxyType.MANUAL
prox.http_proxy = "ip_addr:port"
prox.socks_proxy = "ip_addr:port"
prox.ssl_proxy = "ip_addr:port"

Next, we add the Proxy object to the capabilities with

prox.add_to_capabilities(capabilities)

And then we create the Chrome driver with the capabilities with

driver = webdriver.Chrome(desired_capabilities=capabilities)

to set the proxy settings when starting Chrome.

Conclusion

To run Selenium Webdriver with a proxy in Python, we can use the Proxy class.

Categories
Python Answers

How to call Python function from JavaScript code?

Sometimes, we want to call Python function from JavaScript code.

In this article, we’ll look at how to call Python function from JavaScript code.

How to call Python function from JavaScript code?

To call Python function from JavaScript code, we run the Python script from the Node.js script with spawn.

For instance, we write

const { spawn } = require("child_process");
const temperatures = [];

const sensor = spawn("python", ["sensor.py"]);
sensor.stdout.on("data", (data) => {
  temperatures.push(parseFloat(data));
  console.log(temperatures);
});

to call spawn with 'python' and an array with the arguments which includes the path to the script file to run.

Then we listen for stdout changes with

sensor.stdout.on("data", (data) => {
  temperatures.push(parseFloat(data));
  console.log(temperatures);
});

to get the output from data and so things with it in the callback.

Conclusion

To call Python function from JavaScript code, we run the Python script from the Node.js script with spawn.

Categories
Python Answers

How to extract text from a PDF file using PDFMiner in Python?

Sometimes, we want to extract text from a PDF file using PDFMiner in Python.

In this article, we’ll look at how to extract text from a PDF file using PDFMiner in Python.

How to extract text from a PDF file using PDFMiner in Python?

To extract text from a PDF file using PDFMiner in Python, we can open the PDF file and then we use TextConverter to convert the text into a string.

For instance, we write

from io import StringIO

from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfdocument import PDFDocument
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.pdfpage import PDFPage
from pdfminer.pdfparser import PDFParser

output_string = StringIO()
with open('example.pdf', 'rb') as in_file:
    parser = PDFParser(in_file)
    doc = PDFDocument(parser)
    rsrcmgr = PDFResourceManager()
    device = TextConverter(rsrcmgr, output_string, laparams=LAParams())
    interpreter = PDFPageInterpreter(rsrcmgr, device)
    for page in PDFPage.create_pages(doc):
        interpreter.process_page(page)

print(output_string.getvalue())

to open the example.pdf file with open.

Then we create the PDFParser object with the in_file.

Next, we create a PDFDocument object with the parser.

And then we create the TextConverter object with the PDFResourceManager object rsrcmgr and output_string.

Then we loop through the pages we get from PDFPage.create_pages(doc) with a for loop.

And we call interpreter.process_page with page to parse each page into text.

Then we get the parsed content as a string with output_string.getvalue.

Conclusion

To extract text from a PDF file using PDFMiner in Python, we can open the PDF file and then we use TextConverter to convert the text into a string.