make this as extended and advanced and super good fdn feature rich it should support all to and from and not oleave any types make it support all possible needs and advanced and working and super good 
from flask import Flask, request, jsonify, send_file
from werkzeug.utils import secure_filename
from io import BytesIO
import os
import fitz  # PyMuPDF for PDF operations
from PIL import Image
import docx
import pandas as pd
import openpyxl
from pptx import Presentation

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'uploads'
app.config['CONVERTED_FOLDER'] = 'converted'

# Ensure folders exist
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
os.makedirs(app.config['CONVERTED_FOLDER'], exist_ok=True)

def save_file(file, filename):
    path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
    file.save(path)
    return path

def pdf_to_image(input_path, output_format):
    doc = fitz.open(input_path)
    images = []
    for page_num in range(doc.page_count):
        page = doc.load_page(page_num)
        pix = page.get_pixmap()
        img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
        img_io = BytesIO()
        img.save(img_io, format=output_format.upper())
        img_io.seek(0)
        images.append(img_io)
    doc.close()
    return images

def image_to_pdf(input_path):
    img = Image.open(input_path)
    output = BytesIO()
    img.save(output, format='PDF')
    output.seek(0)
    return output

def pdf_to_word(input_path):
    doc = fitz.open(input_path)
    text = "\n".join([page.get_text() for page in doc])
    doc.close()
    docx_doc = docx.Document()
    docx_doc.add_paragraph(text)
    output = BytesIO()
    docx_doc.save(output)
    output.seek(0)
    return output

def pdf_to_excel(input_path):
    tables = []
    doc = fitz.open(input_path)
    for page in doc:
        tables.append(page.get_text("text"))
    doc.close()
    df = pd.DataFrame([x.split() for x in tables])
    output = BytesIO()
    with pd.ExcelWriter(output, engine='openpyxl') as writer:
        df.to_excel(writer, index=False)
    output.seek(0)
    return output

def pdf_to_text(input_path):
    doc = fitz.open(input_path)
    text = "\n".join([page.get_text() for page in doc])
    doc.close()
    output = BytesIO()
    output.write(text.encode('utf-8'))
    output.seek(0)
    return output

def pdf_to_powerpoint(input_path):
    doc = fitz.open(input_path)
    presentation = Presentation()
    for page_num in range(doc.page_count):
        page = doc.load_page(page_num)
        pix = page.get_pixmap()
        slide = presentation.slides.add_slide(presentation.slide_layouts[5])
        img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
        image_path = f"temp_slide_{page_num}.png"
        img.save(image_path)
        slide.shapes.add_picture(image_path, 0, 0)
        os.remove(image_path)
    output = BytesIO()
    presentation.save(output)
    output.seek(0)
    return output

def convert_file(input_path, from_format, to_format):
    # Define conversion matrix
    conversions = {
        ('pdf', 'png'): lambda path: pdf_to_image(path, 'PNG')[0],  # First page only
        ('pdf', 'jpg'): lambda path: pdf_to_image(path, 'JPEG')[0],
        ('pdf', 'docx'): pdf_to_word,
        ('pdf', 'xlsx'): pdf_to_excel,
        ('pdf', 'txt'): pdf_to_text,
        ('pdf', 'pptx'): pdf_to_powerpoint,
        ('jpg', 'pdf'): image_to_pdf,
        ('png', 'pdf'): image_to_pdf,
        # Add more mappings here
    }

    # Check if conversion is supported
    conversion_func = conversions.get((from_format, to_format))
    if not conversion_func:
        raise ValueError(f"Conversion from {from_format} to {to_format} not supported")

    # Perform conversion
    return conversion_func(input_path)

@app.route('/convert', methods=['POST'])
def convert():
    to_format = request.form.get('to')
    from_format = request.form.get('from')
    input_file = request.files['file']

    if not all([to_format, from_format, input_file]):
        return jsonify({"error": "Missing parameters"}), 400

    # Secure filename and save
    filename = secure_filename(input_file.filename)
    input_path = save_file(input_file, filename)
    
    # Perform conversion
    try:
        output = convert_file(input_path, from_format, to_format)
        output_filename = f"converted.{to_format}"
        output_path = os.path.join(app.config['CONVERTED_FOLDER'], output_filename)

        # Save output file
        with open(output_path, 'wb') as f:
            f.write(output.read())

        return send_file(output_path, as_attachment=True)
    except Exception as e:
        return jsonify({"error": str(e)}), 500
    finally:
        # Cleanup uploaded and output files
        if os.path.exists(input_path):
            os.remove(input_path)
        if os.path.exists(output_path):
            os.remove(output_path)

if __name__ == "__main__":
    app.run(debug=True)