0% found this document useful (0 votes)
26 views

Python p10

The document describes two Python programs: 1. A program to combine selected pages from multiple PDFs into a new output PDF using the PyPDF2 library. The user inputs the page number and the program extracts that page from two source PDFs and combines them into a new output PDF. 2. A program to extract current weather data from a JSON weather data file. The program loads the JSON file, extracts the temperature, humidity, and weather description values, and prints them.

Uploaded by

Sushant K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Python p10

The document describes two Python programs: 1. A program to combine selected pages from multiple PDFs into a new output PDF using the PyPDF2 library. The user inputs the page number and the program extracts that page from two source PDFs and combines them into a new output PDF. 2. A program to extract current weather data from a JSON weather data file. The program loads the JSON file, extracts the temperature, humidity, and weather description values, and prints them.

Uploaded by

Sushant K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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

You might also like