How to use superscript characters (Latex?) with fpdf2 python? - pdf

I hope you are well.
I'm trying to write the following to a pdf using the fpdf2 library:
GHI (kWh $m^-2 day^{-1}$)
But I'm not getting the expected result, the text is written in the pdf in the same way as the code, that is: GHI (kWh $m^-2 day^{-1}$)
Here is a basic example:
from fpdf import FPDF
pdf = FPDF()
pdf.add_page()
pdf.set_font("helvetica", "B", 16)
pdf.cell(40, 10, r"GHI (kWh $m^-2 day^{-1}$)")
pdf.output("test.pdf")
Could someone let me know how I can write this text in the pdf so that it is rendered correctly?
Thank you very much in advance,
Robson

fpdf2 now supports subscript & superscript, as well as <sub> & <sup> HTML tags: https://github.com/PyFPDF/fpdf2/blob/master/CHANGELOG.md#added-1
You can also render mathematical formulas using Google Charts API or Matplotlib: https://pyfpdf.github.io/fpdf2/Maths.html#mathematical-formulas
from io import BytesIO
from urllib.parse import quote
from urllib.request import urlopen
from fpdf import FPDF
formula = r"GHI (kWh $m^-2 day^{-1}$)"
height = 170
url = f"https://chart.googleapis.com/chart?cht=tx&chs={height}&chl={quote(formula)}"
with urlopen(url) as img_file:
img = BytesIO(img_file.read())
pdf = FPDF()
pdf.add_page()
pdf.image(img, w=30)
pdf.output("equation-with-gcharts.pdf")

Related

Need help to make pytesseract could choose folder to save

I need help to make the pytesseract could select or choose folder or location to save the outputt result.
This is the code that I use
from tkinter import filedialog
import pytesseract as tess
from PIL import Image
tess.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
def extract():
files = filedialog.askopenfilenames(title="Select Images")
text = []
n = len(files)
for i in range(0,n):
img = Image.open(files[i])
text.append(tess.image_to_pdf_or_hocr(img, lang='nld', config='--psm 11 --oem 3'))
text_files = open(f'test{i+1}.pdf',"+wb")
text_files.write(text[i])
if __name__ == "__main__":
extract()
I don't have python background but eager to learn more.
Thanks
I have try few code but still not working.
Even better if someone could write and add the code to make it works

How to Fix, AttributeError: 'JpegImageFile' object has no attribute 'load_img'

I'm very new to coding and was coping this code from a youtube video I saw subbing in my own files for images when necessary. I got to the final step of testing the ai on new images and getting a result but this error message pops up.
dir_path = "/content/drive/MyDrive/darth_vader_Bing_images/test 1"
for i in os.listdir(dir_path ):
image = img.load_img(dir_path+'//'+ i)
plt.imshow(img)
plt.show
Sample code
from PIL import Image
import matplotlib.pyplot as plt
img = Image.open('cat1.jpeg')
plt.imshow(img)

Bokeh Server: import .csv file with FileInput widget and pass it to ColumnDataSource

I have a csv file with my data to plot (x, y, and other fields) and want to import it using the new FileInput widget. I don't have sufficient knowledge to manipulate the "base64" strings coming from FileInput to pass it to a ColumnDataSource of dataframe.
from bokeh.io import curdoc
from bokeh.models.widgets import FileInput
def update_cds(attr, old, new):
#A code here to extract column names and data
#from file_input and pass it to a ColumnDataSource or a DataFrame
file_input = FileInput(accept=".csv")
file_input.on_change('value', update_cds)
doc=curdoc()
doc.add_root(file_input)
Thanks for your help!
Here is a working solution: the code will upload the csv file on the server side in a 'data' folder (to be created before). Then it is easy to open the csv and pass it to a ColumnDataSource for instance.
#widget
file_input = FileInput(accept=".csv")
def upload_csv_to_server(attr, old, new):
#decode base64 format (from python base24 website)
base64_message = file_input.value
base64_bytes = base64_message.encode('ascii')
message_bytes = base64.b64decode(base64_bytes)
message = message_bytes.decode('ascii')
#convert string to csv and save it on the server side
message_list = message.splitlines()
with open('data/' + file_input.filename, 'w', newline='') as file:
writer = csv.writer(file)
for i in range(len(message_list)):
writer.writerow(message_list[i].split(','))
file_input.on_change('value', upload_csv_to_server)
If you see a nicer way please let me know. It is easy with the csv structure to do that way but what about any other file format?
Python has a built in base64 standard library module:
import base64
data = base64.b64decode(encoded)

input custom text in youtube text field using selenium python

I'm making a text scraper for youtube in which I want to enter data and search videos and collect data of it. I'm facing problems in entering data in the text field. Can anyone suggest me a method to do that?
from bs4 import BeautifulSoup
driver = webdriver.Chrome()
soup = BeautifulSoup(driver.page_source, 'lxml') #Use the page as source
page = driver.get('https://freight.rivigo.com/dashboard/home')
import sys
from importlib import reload
reload
elem = driver.find_element_by_tag_name("body")
no_of_pagedowns = 120
while no_of_pagedowns:
elem.send_keys(Keys.PAGE_DOWN)
time.sleep(0.5)
no_of_pagedowns-=1
soup = BeautifulSoup(driver.page_source, 'lxml')
In between this code I want to add a custom text in input field, lets say "comedy" and want to get data on that. I'm stuck on how to input data and I'm quite new to this so any sort of help will be helpful.
That page is NOT pointing to YouTube. Check out the working code sample below for an idea of what you can do with the YouTube API.
# https://medium.com/greyatom/youtube-data-in-python-6147160c5833
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
#from youtube_data import youtube_search
test = youtube_search("Nine Inch Nails")
test.keys()
test['commentCount'][:5]
df = pd.DataFrame(data=test)
df.head()
df1 = df[['title','viewCount','channelTitle','commentCount','likeCount','dislikeCount','tags','favoriteCount','videoId','channelId','categoryId']]
df1.columns = ['Title','viewCount','channelTitle','commentCount','likeCount','dislikeCount','tags','favoriteCount','videoId','channelId','categoryId']
df1.head()
#import numpy as np
#numeric_dtype = ['viewCount','commentCount','likeCount','dislikeCount','favoriteCount']
#for i in numeric_dtype:
# df1[i] = df[i].astype(int)
NIN = df1[df1['channelTitle']=='Nine Inch Nails']
NIN.head()
NIN = NIN.sort_values(ascending=False,by='viewCount')
plt.bar(range(NIN.shape[0]),NIN['viewCount'])
plt.xticks(range(NIN.shape[0]),NIN['Title'],rotation=90)
plt.ylabel('viewCount in 100 millions')
plt.show()

Beautifulsoup turning <link> into <link/>

I'm trying to download and parse text from some RSS feeds, such as http://rss.sciencedirect.com/publication/science/03043878. Here's a simple example:
import urllib.request
import urllib.parse
import requests
from bs4 import BeautifulSoup
def main():
soup = BeautifulSoup(urllib.request.urlopen('http://rss.sciencedirect.com/publication/science/03043878'),"html.parser").encode("ascii")
print(soup)
if __name__ == '__main__':
main()
In the original html (if you look at the website directly), links are preceded by <link> and followed by </link>. But what beautifulsoup prints out replaces <link> with <link/> and drops </link> completely. Any ideas what I could be doing wrong or is this a bug?
PS Tried changing encoding to utf-8 and it still happens.
You are parsing RSS. RSS is XML. So pass features="xml" into the BeautifulSoup constructor.
import urllib.request
from bs4 import BeautifulSoup
def main():
doc = BeautifulSoup(urllib.request.urlopen('http://rss.sciencedirect.com/publication/science/03043878'), "xml")
# If you want to print it as ascii (as per your original post).
print (doc.prettify('ascii'))
# To write it to an file as ascii (as per your original post).
with open("ascii.txt", "wb") as file:
file.write(doc.prettify('ascii'))
# To write it to an file as utf-8 (as the original RSS).
with open("utf-8.txt", "wb") as file:
file.write(doc.prettify('utf-8'))
# If you want to print the links.
for item in doc.findAll('link'):
print(item)
if __name__ == '__main__':
main()
Outputs in both files and terminal:
... <link>
http://rss.sciencedirect.com/action/redirectFile?&zone=main&currentActivity=feed&usageType=outward&url=http%3A%2F%2Fwww.sciencedirect.com%2Fscience%3F_ob%3DGatewayURL%26_origin%3DIRSSSEARCH%26_method%3DcitationSearch%26_piikey%3DS0304387817300512%26_version%3D1%26md5%3D16ed8e2672e8048590d3c41993306b0f
</link> ...
The parser can't evaluate the links correctly. For this issue you should use xml as your parser instead of html.parser.
soup = BeautifulSoup(urllib.request.urlopen('http://rss.sciencedirect.com/publication/science/03043878'),"xml")
print(len(soup.find_all("link")))
Outputs 52 links.