Posts

Extract Tables from a PDF and Save into a CSV

# first of all install the 2 dependencies # pip install tk # pip install ghostscript # and then # pip install camelot-py import camelot table = camelot.read_pdf("table.pdf", pages="1") print(table) # exports the list of tables to specified file format table.export("mytab.csv", f="csv", compress=True) # only the specified table is exported table[0].to_csv("mytab.csv")

Extract Tables from a Web Page in Python

import pandas as pd xfiles = pd.read_html("https://en.wikipedia.org/wiki/List_of_The_X-Files_episodes") print(xfiles) # prints all the tables on the page as list print(type(xfiles)) print(len(xfiles)) print(xfiles[1]) # table of season 1 print(xfiles[12]) # table of season 10

Read CSV from a Web Page

import pandas as pd # the link is taken from right-clicking the csv file on a web page and 'copy link address' footballData = pd.read_csv("https://datahub.io/sports-data/english-premier-league/r/season-1819.csv") # print(footballData) # renaming two columns names; the inplace changes the original footballData.rename(columns={"FTHG": "Home", "FTAG": "Abroad"}, inplace=True) print(footballData)

Decorators in Python

def greet(fx): # decorator function def mfx(*args, **kwargs): print("Welcome to this function!") fx(*args, **kwargs) print("Thanks for using this function.") return mfx @greet def add(a,b): print("The sum of your numbers is", a+b) @greet def thisWorld(): print("This world is beautiful.") # add(2,4) # print() # just to print a new line or blank line # thisWorld() # the above decorators can also be implemented in the following way... greet(thisWorld()) # == greet(thisWorld)() greet(add(3,4)) # == greet(add)(3,4)

Plotting Graphs in Python with Matplotlib Library

import matplotlib.pyplot as plt # one line graph plotting x = [1,2,3,4,5] y = [320, 400, 650, 456, 870] plt.plot(x,y) # customizing your graph # plt.plot( # x, y, color="red", linestyle="dashed", # linewidth=2, marker="o", markerfacecolor="cyan", # markersize=14 # ) # plt.xlim(1,10) # x axis limits # plt.ylim(1,1000) # y axis limits plt.xlabel("Months") plt.ylabel("Gas Bills") plt.title("Gas bills of the months") plt.show() ############################################ # two-lines graph plotting x1 = [1,2,3,4,5] y1 = [320, 400, 650, 456, 870] plt.plot(x1,y1, label="Gas Bills") x2 = [1,2,3,4,5] y2 = [620, 800, 450, 556, 270] plt.plot(x2,y2, label="Water Bills") plt.xlabel("Months") plt.ylabel("Billing Amount") plt.title("Bills of the months") plt.legend() plt.show() ############################################ # creating a bar chart player

Image Resizing Script in Python

from PIL import Image #pip install Pillow def resize_your_image(width, height): image_address = "new-img.jpg" image = Image.open(image_address) print("The original size of your image is",image.size) image_resize = image.resize((width, height)) print("Image successfully resized to",image_resize.size) image_resize.save(f"r-img-{image_resize.size}.jpg") print("Image successfully saved.") print("Welcome to Image Resizer!") width=int(input("Plz enter the required width: ")) height=int(input("Plz enter the required height: ")) resize_your_image(width, height)

Scraping Post Titles from a Blog Page in Python

import requests #pip install requests from bs4 import BeautifulSoup #pip install beautifulsoup4 url = "https://www.pharmacymcqs.com/" resposne = requests.get(url) # print(resposne) soup = BeautifulSoup(resposne.content, "lxml") # print(soup) post_titles = soup.find_all("h2", {"class":"post-title"}) # to print only the first title # print(post_titles[0].getText()) # to print all the titles from the blog page for title in post_titles: print(title.getText())