10.
a) Write a python program to combine select pages from many PDFs
PyPDF2 installation commands:
1. sudo apt install python3-pip
2. sudo pip3 install PyPDF2
Program:
from PyPDF2 import PdfWriter, PdfReader
num = int(input("Enter page number you want combine from multiple documents :"))
pdf1 = open('Python_p6.pdf', 'rb')
pdf2 = open('manual.pdf', 'rb')
pdf_writer = PdfWriter()
pdf1_reader = PdfReader(pdf1)
page = pdf1_reader.pages[num - 1]
pdf_writer.add_page(page)
pdf2_reader = PdfReader(pdf2)
page = pdf2_reader.pages[num - 1]
pdf_writer.add_page(page)
with open('output.pdf', 'wb') as output:
pdf_writer.write(output)
OUTPUT:
Enter page number you want combine from multiple documents : 2
Note: it will combine the 2nd page of Python_p6.pdf with the 2nd page of manual.pdf and
create a new pdf with the name output.pdf, in your directory.
10. b) Write a python program to fetch current weather data from the JSON file
1. Create a json file in your directory with the name : Weather.json type and save the
below program.
{
"coord": {
"lon": -73.99,
"lat": 40.73
},
"weather": [
{
"id": 800,
"main": "Clear",
"description": "clear sky",
"icon": "01d"
}
],
"base": "stations",
"main": {
"temp": 15.45,
"feels_like": 12.74,
"temp_min": 14.44,
"temp_max": 16.11,
"pressure": 1017,
"humidity": 64
},
"visibility": 10000,
"wind": {
"speed": 4.63,
"deg": 180
},
"clouds": {
"all": 1
},
"dt": 1617979985,
"sys": {
"type": 1,
"id": 5141,
"country": "US",
"sunrise": 1617951158,
"sunset": 1618000213
},
"timezone": -14400,
"id": 5128581,
"name": "New York",
"cod": 200
}
2. Now create actual program file as Program_10b.py
Program:
import json
# Load the JSON data from file
with open('example.json') as f:
data = json.load(f)
# Extract the required weather data
current_temp = data['main']['temp']
humidity = data['main']['humidity']
weather_desc = data['weather'][0]['description']
# Display the weather data
print(f"Current temperature: {current_temp}°C")
print(f"Humidity: {humidity}%")
print(f"Weather description: {weather_desc}")
OUTPUT:
Current temperature: 15.45°C
Humidity: 64%
Weather description: clear sky