Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| from PyPDF2 import PdfReader | |
| from docx import Document | |
| from pdf2image import convert_from_bytes | |
| import pytesseract | |
| from datetime import datetime | |
| from PIL import Image | |
| import io | |
| # Configure Tesseract path (if needed) | |
| # pytesseract.pytesseract.tesseract_cmd = r'/path/to/tesseract' | |
| # Page Configuration | |
| st.set_page_config(page_title="Everything Converter", page_icon="🔄", layout="centered") | |
| # App Header | |
| st.title("🔄 Everything Converter") | |
| st.write("Your one-stop solution for all types of conversions!") | |
| # Sidebar for Navigation | |
| st.sidebar.title("Navigation") | |
| conversion_type = st.sidebar.radio( | |
| "Choose a Converter", | |
| ["Unit Converter", "File Converter", "Image Converter", "Date and Time Converter", "Number System Converter"] | |
| ) | |
| # Unit Converter | |
| if conversion_type == "Unit Converter": | |
| st.header("Unit Converter") | |
| unit_category = st.selectbox("Select Unit Category", ["Length", "Weight", "Temperature"]) | |
| if unit_category == "Length": | |
| length_units = ["Meters", "Kilometers", "Miles", "Feet", "Inches"] | |
| from_unit = st.selectbox("From", length_units) | |
| to_unit = st.selectbox("To", length_units) | |
| value = st.number_input("Enter Value", min_value=0.0) | |
| if st.button("Convert"): | |
| # Conversion logic for length | |
| conversions = { | |
| "Meters": 1, | |
| "Kilometers": 0.001, | |
| "Miles": 0.000621371, | |
| "Feet": 3.28084, | |
| "Inches": 39.3701, | |
| } | |
| result = value * (conversions[to_unit] / conversions[from_unit]) | |
| st.success(f"Converted Value: **{result:.2f} {to_unit}**") | |
| elif unit_category == "Weight": | |
| weight_units = ["Kilograms", "Grams", "Pounds", "Ounces"] | |
| from_unit = st.selectbox("From", weight_units) | |
| to_unit = st.selectbox("To", weight_units) | |
| value = st.number_input("Enter Value", min_value=0.0) | |
| if st.button("Convert"): | |
| # Conversion logic for weight | |
| conversions = { | |
| "Kilograms": 1, | |
| "Grams": 1000, | |
| "Pounds": 2.20462, | |
| "Ounces": 35.274, | |
| } | |
| result = value * (conversions[to_unit] / conversions[from_unit]) | |
| st.success(f"Converted Value: **{result:.2f} {to_unit}**") | |
| elif unit_category == "Temperature": | |
| temp_units = ["Celsius", "Fahrenheit", "Kelvin"] | |
| from_unit = st.selectbox("From", temp_units) | |
| to_unit = st.selectbox("To", temp_units) | |
| value = st.number_input("Enter Value", min_value=-273.15) | |
| if st.button("Convert"): | |
| # Conversion logic for temperature | |
| if from_unit == "Celsius": | |
| if to_unit == "Fahrenheit": | |
| result = (value * 9/5) + 32 | |
| elif to_unit == "Kelvin": | |
| result = value + 273.15 | |
| else: | |
| result = value | |
| elif from_unit == "Fahrenheit": | |
| if to_unit == "Celsius": | |
| result = (value - 32) * 5/9 | |
| elif to_unit == "Kelvin": | |
| result = (value - 32) * 5/9 + 273.15 | |
| else: | |
| result = value | |
| elif from_unit == "Kelvin": | |
| if to_unit == "Celsius": | |
| result = value - 273.15 | |
| elif to_unit == "Fahrenheit": | |
| result = (value - 273.15) * 9/5 + 32 | |
| else: | |
| result = value | |
| st.success(f"Converted Value: **{result:.2f} {to_unit}**") | |
| # File Converter | |
| elif conversion_type == "File Converter": | |
| st.header("File Converter") | |
| file_type = st.selectbox("Select File Type", ["PDF to Word", "Image to PDF"]) | |
| if file_type == "PDF to Word": | |
| uploaded_file = st.file_uploader("Upload a PDF file", type="pdf") | |
| if uploaded_file is not None: | |
| if st.button("Convert to Word"): | |
| reader = PdfReader(uploaded_file) | |
| document = Document() | |
| for page in reader.pages: | |
| text = page.extract_text() | |
| document.add_paragraph(text) | |
| word_file = "converted.docx" | |
| document.save(word_file) | |
| st.success("Conversion successful!") | |
| st.download_button("Download Word File", word_file, file_name="converted.docx") | |
| elif file_type == "Image to PDF": | |
| uploaded_file = st.file_uploader("Upload an Image file", type=["jpg", "jpeg", "png"]) | |
| if uploaded_file is not None: | |
| if st.button("Convert to PDF"): | |
| images = convert_from_bytes(uploaded_file.read()) | |
| pdf_file = "converted.pdf" | |
| images[0].save(pdf_file, "PDF") | |
| st.success("Conversion successful!") | |
| st.download_button("Download PDF File", pdf_file, file_name="converted.pdf") | |
| # Image Converter | |
| elif conversion_type == "Image Converter": | |
| st.header("Image Converter") | |
| image_type = st.selectbox("Select Conversion Type", ["JPG to PNG", "PNG to JPG"]) | |
| uploaded_file = st.file_uploader("Upload an Image file", type=["jpg", "jpeg", "png"]) | |
| if uploaded_file is not None: | |
| if st.button("Convert"): | |
| image = Image.open(uploaded_file) | |
| if image_type == "JPG to PNG": | |
| output_format = "PNG" | |
| output_file = "converted.png" | |
| elif image_type == "PNG to JPG": | |
| output_format = "JPEG" | |
| output_file = "converted.jpg" | |
| # Save the converted image | |
| image.save(output_file, output_format) | |
| st.success("Conversion successful!") | |
| st.download_button(f"Download {output_format} File", output_file, file_name=output_file) | |
| # Image Resizer | |
| elif conversion_type == "Image Resizer": | |
| st.header("Image Resizer") | |
| uploaded_file = st.file_uploader("Upload an Image file", type=["jpg", "jpeg", "png"]) | |
| if uploaded_file is not None: | |
| width = st.number_input("Enter Width", min_value=1, value=500) | |
| height = st.number_input("Enter Height", min_value=1, value=500) | |
| if st.button("Resize"): | |
| image = Image.open(uploaded_file) | |
| resized_image = image.resize((width, height)) | |
| resized_file = "resized_image.png" | |
| resized_image.save(resized_file, "PNG") | |
| st.success("Resizing successful!") | |
| st.download_button("Download Resized Image", resized_file, file_name=resized_file) | |
| # Date and Time Converter | |
| elif conversion_type == "Date and Time Converter": | |
| st.header("Date and Time Converter") | |
| date_input = st.date_input("Select a Date") | |
| time_input = st.time_input("Select a Time") | |
| datetime_obj = datetime.combine(date_input, time_input) | |
| st.write("Converted Date and Time:") | |
| st.write(f"ISO Format: **{datetime_obj.isoformat()}**") | |
| st.write(f"Timestamp: **{datetime_obj.timestamp()}**") | |
| # Number System Converter | |
| elif conversion_type == "Number System Converter": | |
| st.header("Number System Converter") | |
| number = st.number_input("Enter a Number", min_value=0) | |
| number_system = st.selectbox("Convert to", ["Binary", "Hexadecimal", "Octal"]) | |
| if st.button("Convert"): | |
| if number_system == "Binary": | |
| result = bin(number) | |
| elif number_system == "Hexadecimal": | |
| result = hex(number) | |
| elif number_system == "Octal": | |
| result = oct(number) | |
| st.success(f"Converted Value: **{result}**") | |
| # Footer | |
| st.markdown("---") | |
| st.write("© 2023 Everything Converter. All rights reserved.") |