diff --git a/ch_nandu/task1/task1.py b/ch_nandu/task1/task1.py new file mode 100644 index 0000000..a4c9d4a --- /dev/null +++ b/ch_nandu/task1/task1.py @@ -0,0 +1,30 @@ +#a simple python program to perform basic tasks like addition,subtraction,multiplication,division +print('please select any of the number for performing arithmetic operations') +print("1.Addition") +print('2.Subtraction') +print('3.Multiplication') +print('4.Division') +print('5.exit') +a=int(input('Enter any of the number for performing arithmetic operations')) +def ari(a,var1,var2): + a,var1,var2=a,var1,var2 + if(a==1): + print(var1+var2) + if(a==2): + print(var1-var2) + if(a==3): + print(var1*var2) + if(a==4): + print(var1/var2) + return + +#Enter Two numbers +if((a>0) and (a<5)): + var1 = int(input('Enter First number: ')) + var2 = int(input('Enter Second number: ')) + ari(a,var1,var2) +elif(a==5): + exit() +else: + print('Invalid Option') + print('please select 1/2/3/4/5 only') diff --git a/ch_nandu/task2/task2.py b/ch_nandu/task2/task2.py new file mode 100644 index 0000000..1eacb0a --- /dev/null +++ b/ch_nandu/task2/task2.py @@ -0,0 +1,59 @@ +class TaskManager: + def __init__(self): + self.tasks = [] + + def add_task(self, task): + self.tasks.append({"task": task, "completed": False}) + + def delete_task(self, index): + if 0 <= index < len(self.tasks): + del self.tasks[index] + else: + print("Invalid task index") + + def mark_task_completed(self, index): + if 0 <= index < len(self.tasks): + self.tasks[index]["completed"] = True + else: + print("Invalid task index") + + def display_tasks(self): + print("Tasks:") + for i, task in enumerate(self.tasks): + status = "Completed" if task["completed"] else "Pending" + print(f"{i+1}. {task['task']} - {status}") + + +def main(): + task_manager = TaskManager() + + while True: + print("\nOptions:") + print("1. Add Task") + print("2. Delete Task") + print("3. Mark Task as Completed") + print("4. View Tasks") + print("5. Exit") + + choice = input("Enter your choice: ") + + if choice == "1": + task = input("Enter the task: ") + task_manager.add_task(task) + elif choice == "2": + index = int(input("Enter the index of the task to delete: ")) - 1 + task_manager.delete_task(index) + elif choice == "3": + index = int(input("Enter the index of the task to mark as completed: ")) - 1 + task_manager.mark_task_completed(index) + elif choice == "4": + task_manager.display_tasks() + elif choice == "5": + print("Exiting...") + break + else: + print("Invalid choice. Please try again.") + + +if __name__ == "__main__": + main() diff --git a/ch_nandu/task3/task3.py b/ch_nandu/task3/task3.py new file mode 100644 index 0000000..f1bc4c2 --- /dev/null +++ b/ch_nandu/task3/task3.py @@ -0,0 +1,33 @@ +import random + +def guess_number(): + # Generate a random number between 1 and 100 + secret_number = random.randint(1, 100) + attempts = 0 + max_attempts = 10 + + print("Welcome to the Number Guessing Game!") + print("I have chosen a number between 1 and 100. You have", max_attempts, "attempts to guess it.") + + while attempts < max_attempts: + try: + guess = int(input("Enter your guess: ")) + except ValueError: + print("Invalid input! Please enter a valid number.") + continue + + attempts += 1 + + if guess < secret_number: + print("Too low! Try again.") + elif guess > secret_number: + print("Too high! Try again.") + else: + print("Congratulations! You've guessed the number", secret_number, "correctly in", attempts, "attempts!") + break + else: + print("Sorry, you've run out of attempts. The correct number was", secret_number) + +if __name__ == "__main__": + guess_number() + diff --git a/ch_nandu/task4/task4.py b/ch_nandu/task4/task4.py new file mode 100644 index 0000000..2b9f9ea --- /dev/null +++ b/ch_nandu/task4/task4.py @@ -0,0 +1,93 @@ +import PyPDF2 +from PIL import Image +import os + + +def convert_pdf_to_text(pdf_path, text_output_path): + """Converts a PDF file to text. + + Args: + pdf_path (str): Path to the input PDF file. + text_output_path (str): Path to save the converted text file. + """ + try: + with open(pdf_path, 'rb') as pdf_file: + pdf_reader = PyPDF2.PdfReader(pdf_file) + with open(text_output_path, 'w', encoding='utf-8') as text_file: + # Iterate through each page of the PDF + for page_num in range(len(pdf_reader.pages)): + page = pdf_reader.pages[page_num] + # Extract text from the page and write it to the text file + text_file.write(page.extract_text()) + print(f"PDF converted to text successfully. Text file saved at {text_output_path}") + except Exception as e: + print(f"An error occurred: {e}") + + +def extract_images_from_pdf(pdf_path, image_output_folder): + """Extracts images from a PDF file. + + Args: + pdf_path (str): Path to the input PDF file. + image_output_folder (str): Folder to save the extracted images. + """ + try: + with open(pdf_path, 'rb') as pdf_file: + pdf_reader = PyPDF2.PdfReader(pdf_file) + # Iterate through each page of the PDF + for page_num in range(len(pdf_reader.pages)): + page = pdf_reader.pages[page_num] + xObject = page['/Resources']['/XObject'].getObject() + for obj in xObject: + if xObject[obj]['/Subtype'] == '/Image': + size = (xObject[obj]['/Width'], xObject[obj]['/Height']) + data = xObject[obj]._data + mode = '' + if xObject[obj]['/ColorSpace'] == '/DeviceRGB': + mode = "RGB" + else: + mode = "P" + if xObject[obj]['/Filter'] == '/FlateDecode': + img = Image.frombytes(mode, size, data) + img.save(os.path.join(image_output_folder, f"page{page_num+1}_{obj[1:]}.png")) + elif xObject[obj]['/Filter'] == '/DCTDecode': + img = open(os.path.join(image_output_folder, f"page{page_num+1}_{obj[1:]}.jpg"), "wb") + img.write(data) + img.close() + elif xObject[obj]['/Filter'] == '/JPXDecode': + img = open(os.path.join(image_output_folder, f"page{page_num+1}_{obj[1:]}.jp2"), "wb") + img.write(data) + img.close() + print(f"Images extracted successfully. Saved in {image_output_folder}") + except Exception as e: + print(f"An error occurred: {e}") + + +def main(): + # Get input paths and output folder from user + pdf_path = input("Enter the path to the PDF file: ") + output_folder = input("Enter the output folder path: ") + + # Create the output folder if it does not exist + if not os.path.exists(output_folder): + os.makedirs(output_folder) + + # Choose conversion option + choice = input("Choose an option:\n1. Convert PDF to text\n2. Extract images from PDF\nEnter your choice: ") + + if choice == '1': + # Convert PDF to text + text_output_path = os.path.join(output_folder, "converted_text.txt") + convert_pdf_to_text(pdf_path, text_output_path) + elif choice == '2': + # Extract images from PDF + image_output_folder = os.path.join(output_folder, "extracted_images") + if not os.path.exists(image_output_folder): + os.makedirs(image_output_folder) + extract_images_from_pdf(pdf_path, image_output_folder) + else: + print("Invalid choice. Please choose 1 or 2.") + + +if __name__ == "__main__": + main()