Skip to content
This repository was archived by the owner on Jun 29, 2024. It is now read-only.

Commit cc8c4b6

Browse files
authored
Add files via upload
1 parent b5be041 commit cc8c4b6

File tree

4 files changed

+212
-0
lines changed

4 files changed

+212
-0
lines changed

Calc.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#a simple python program to perform basic tasks like addition,subtraction,multiplication,division
2+
print('please select any of the number for performing arithmetic operations')
3+
print("1.Addition")
4+
print('2.Subtraction')
5+
print('3.Multiplication')
6+
print('4.Division')
7+
print('5.exit')
8+
a=int(input('Enter any of the number for performing arithmetic operations'))
9+
def ari(a,var1,var2):
10+
a,var1,var2=a,var1,var2
11+
if(a==1):
12+
print(var1+var2)
13+
if(a==2):
14+
print(var1-var2)
15+
if(a==3):
16+
print(var1*var2)
17+
if(a==4):
18+
print(var1/var2)
19+
return
20+
21+
#Enter Two numbers
22+
if((a>0) and (a<5)):
23+
var1 = int(input('Enter First number: '))
24+
var2 = int(input('Enter Second number: '))
25+
ari(a,var1,var2)
26+
elif(a==5):
27+
exit()
28+
else:
29+
print('Invalid Option')
30+
print('please select 1/2/3/4/5 only')

number guessing.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import random
2+
3+
def guess_number():
4+
# Generate a random number between 1 and 100
5+
secret_number = random.randint(1, 100)
6+
attempts = 0
7+
max_attempts = 10
8+
9+
print("Welcome to the Number Guessing Game!")
10+
print("I have chosen a number between 1 and 100. You have", max_attempts, "attempts to guess it.")
11+
12+
while attempts < max_attempts:
13+
try:
14+
guess = int(input("Enter your guess: "))
15+
except ValueError:
16+
print("Invalid input! Please enter a valid number.")
17+
continue
18+
19+
attempts += 1
20+
21+
if guess < secret_number:
22+
print("Too low! Try again.")
23+
elif guess > secret_number:
24+
print("Too high! Try again.")
25+
else:
26+
print("Congratulations! You've guessed the number", secret_number, "correctly in", attempts, "attempts!")
27+
break
28+
else:
29+
print("Sorry, you've run out of attempts. The correct number was", secret_number)
30+
31+
if __name__ == "__main__":
32+
guess_number()
33+

pdfconv.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import PyPDF2
2+
from PIL import Image
3+
import os
4+
5+
def convert_pdf_to_text(pdf_path, text_output_path):
6+
"""Converts a PDF file to text.
7+
8+
Args:
9+
pdf_path (str): Path to the input PDF file.
10+
text_output_path (str): Path to save the converted text file.
11+
"""
12+
try:
13+
with open(pdf_path, 'rb') as pdf_file:
14+
pdf_reader = PyPDF2.PdfReader(pdf_file)
15+
with open(text_output_path, 'w', encoding='utf-8') as text_file:
16+
# Iterate through each page of the PDF
17+
for page_num in range(len(pdf_reader.pages)):
18+
page = pdf_reader.pages[page_num]
19+
# Extract text from the page and write it to the text file
20+
text_file.write(page.extract_text())
21+
print(f"PDF converted to text successfully. Text file saved at {text_output_path}")
22+
except Exception as e:
23+
print(f"An error occurred: {e}")
24+
25+
26+
def extract_images_from_pdf(pdf_path, image_output_folder):
27+
"""Extracts images from a PDF file.
28+
29+
Args:
30+
pdf_path (str): Path to the input PDF file.
31+
image_output_folder (str): Folder to save the extracted images.
32+
"""
33+
try:
34+
with open(pdf_path, 'rb') as pdf_file:
35+
pdf_reader = PyPDF2.PdfReader(pdf_file)
36+
# Iterate through each page of the PDF
37+
for page_num in range(len(pdf_reader.pages)):
38+
page = pdf_reader.pages[page_num]
39+
xObject = page.resources['XObject'].resolve()
40+
for obj in xObject:
41+
if xObject[obj]['/Subtype'] == '/Image':
42+
size = (xObject[obj]['/Width'], xObject[obj]['/Height'])
43+
data = xObject[obj].get_data()
44+
mode = ''
45+
if xObject[obj]['/ColorSpace'] == '/DeviceRGB':
46+
mode = "RGB"
47+
else:
48+
mode = "P"
49+
if xObject[obj]['/Filter'] == '/FlateDecode':
50+
img = Image.frombytes(mode, size, data)
51+
img.save(os.path.join(image_output_folder, f"page{page_num+1}_{obj[1:]}.png"))
52+
elif xObject[obj]['/Filter'] == '/DCTDecode':
53+
img = open(os.path.join(image_output_folder, f"page{page_num+1}_{obj[1:]}.jpg"), "wb")
54+
img.write(data)
55+
img.close()
56+
elif xObject[obj]['/Filter'] == '/JPXDecode':
57+
img = open(os.path.join(image_output_folder, f"page{page_num+1}_{obj[1:]}.jp2"), "wb")
58+
img.write(data)
59+
img.close()
60+
print(f"Images extracted successfully. Saved in {image_output_folder}")
61+
except Exception as e:
62+
print(f"An error occurred: {e}")
63+
64+
def main():
65+
# Get input paths and output folder from user
66+
pdf_path = input("Enter the path to the PDF file: ")
67+
output_folder = input("Enter the output folder path: ")
68+
69+
# Create the output folder if it does not exist
70+
if not os.path.exists(output_folder):
71+
os.makedirs(output_folder)
72+
73+
# Choose conversion option
74+
choice = input("Choose an option:\n1. Convert PDF to text\n2. Extract images from PDF\nEnter your choice: ")
75+
76+
if choice == '1':
77+
# Convert PDF to text
78+
text_output_path = os.path.join(output_folder, "converted_text.txt")
79+
convert_pdf_to_text(pdf_path, text_output_path)
80+
elif choice == '2':
81+
# Extract images from PDF
82+
image_output_folder = os.path.join(output_folder, "extracted_images")
83+
if not os.path.exists(image_output_folder):
84+
os.makedirs(image_output_folder)
85+
extract_images_from_pdf(pdf_path, image_output_folder)
86+
else:
87+
print("Invalid choice. Please choose 1 or 2.")
88+
89+
if __name__ == "__main__":
90+
main()

to_do_list.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
class TaskManager:
2+
def __init__(self):
3+
self.tasks = []
4+
5+
def add_task(self, task):
6+
self.tasks.append({"task": task, "completed": False})
7+
8+
def delete_task(self, index):
9+
if 0 <= index < len(self.tasks):
10+
del self.tasks[index]
11+
else:
12+
print("Invalid task index")
13+
14+
def mark_task_completed(self, index):
15+
if 0 <= index < len(self.tasks):
16+
self.tasks[index]["completed"] = True
17+
else:
18+
print("Invalid task index")
19+
20+
def display_tasks(self):
21+
print("Tasks:")
22+
for i, task in enumerate(self.tasks):
23+
status = "Completed" if task["completed"] else "Pending"
24+
print(f"{i+1}. {task['task']} - {status}")
25+
26+
27+
def main():
28+
task_manager = TaskManager()
29+
30+
while True:
31+
print("\nOptions:")
32+
print("1. Add Task")
33+
print("2. Delete Task")
34+
print("3. Mark Task as Completed")
35+
print("4. View Tasks")
36+
print("5. Exit")
37+
38+
choice = input("Enter your choice: ")
39+
40+
if choice == "1":
41+
task = input("Enter the task: ")
42+
task_manager.add_task(task)
43+
elif choice == "2":
44+
index = int(input("Enter the index of the task to delete: ")) - 1
45+
task_manager.delete_task(index)
46+
elif choice == "3":
47+
index = int(input("Enter the index of the task to mark as completed: ")) - 1
48+
task_manager.mark_task_completed(index)
49+
elif choice == "4":
50+
task_manager.display_tasks()
51+
elif choice == "5":
52+
print("Exiting...")
53+
break
54+
else:
55+
print("Invalid choice. Please try again.")
56+
57+
58+
if __name__ == "__main__":
59+
main()

0 commit comments

Comments
 (0)