0% found this document useful (0 votes)
9 views3 pages

Python Scripts for Data Processing Tasks

The document contains multiple code snippets for different tasks. It includes a program for calculating average marks from user input, downloading a comic from XKCD, reading and writing data to an Excel spreadsheet, and combining PDF files. Each snippet demonstrates basic programming concepts and file handling in Python.

Uploaded by

Harshith S
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)
9 views3 pages

Python Scripts for Data Processing Tasks

The document contains multiple code snippets for different tasks. It includes a program for calculating average marks from user input, downloading a comic from XKCD, reading and writing data to an Excel spreadsheet, and combining PDF files. Each snippet demonstrates basic programming concepts and file handling in Python.

Uploaded by

Harshith S
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

1a

try:
m1
= int(input("Enter mark 1 :"))
m2
= int(input("Enter mark 2 :"))
m3
= int(input("Enter mark 3 :"))
if
m1 > m2 > m3 or m2 > m1 > m3:
print(f'The best of 2 subjects are : {m1} & {m2}')
avg = (m1+m2)/2
elif m3 > m2 > m1 or m2 > m3 > m1:
print(f'The best of 2 subjects are : {m2} & {m3}')
avg = (m3+m2)/2
else:
print(f'The best of 2 subjects are : {m1} & {m3}')
avg = (m3+m1)/2
print(f'Average: {avg}')
except ValueError as e:
print(e)

9a

import requests
import os,bs4

url='[Link]
n = input("Enter comic number")
[Link]('xkcd',exist_ok=True)
url+=n
print(f"Downlaoding {url}")
response = [Link](url)
if response.status_code!=200:
exit()
soup = [Link]([Link],'[Link]')
comic_element = [Link]('#comic img')
comic_url = comic_element[0].get('src')
comic_url = 'http:'+comic_url
response = [Link](comic_url)
with open([Link]('xkcd',[Link](comic_url)),'wb') as image:
for chunk in response.iter_content(100000):
[Link](chunk)
print("Image downloaded successfully")
9b

import openpyxl
def read_spreadsheet(filename):
workbook = openpyxl.load_workbook(filename)
sheet = [Link]

data = []
for row in sheet.iter_rows(values_only=True):
[Link](row)
return data

def write_spreadsheet(filename,data):
workbook = [Link]()
sheet = [Link]

for row in data:


[Link](row)
[Link](filename)

data = [
("id", "name", "age"),
("1", "arun", "18"),
("2", "tarun", "20"),
("3", "ammu", "25"),

]
# writing data to excel sheet, this will create new excel sheet so that we can
read this further
write_spreadsheet('[Link]',data)

# now i can read this excel sheet


read_data = read_spreadsheet('[Link]')
id = int(input("Enter id of the student whose details has to be shown: "))
print(f'Name: {read_data[id][1]}\nAge: {read_data[id][2]}')
10 a

from PyPDF2 import PdfWriter, PdfReader

def combine_pdfs(file1, file2, output, num):


pdf1 = open(file1, 'rb')
pdf2 = open(file2, 'rb')
pdf1_reader = PdfReader(pdf1)
pdf2_reader = PdfReader(pdf2)

writer = PdfWriter()

# Add pages from pdf1 up to the specified page number


for page in pdf1_reader.pages[:num]:
writer.add_page(page)

# Add pages from pdf2 up to the specified page number


for page in pdf2_reader.pages[:num]:
writer.add_page(page)

# Write the combined pages to the output PDF file


with open(output, 'wb') as output_file:
[Link](output_file)

[Link]()
[Link]()

combine_pdfs('[Link]', '[Link]', '[Link]', 1)

You might also like