from sqlalchemy import create_engine
import pandas as pd
import time,os

PathToSaveCSV=r"u:\NextCloudOrdinace\Reporty\testy"
timestr = time.strftime("%Y-%m-%d %H-%M-%S ")
reportname=os.path.join(PathToSaveCSV,(timestr+"Výkony.xlsx"))
print(reportname)

# Create connection string
connection_string = (
    "firebird+fdb://SYSDBA:masterkey@localhost/u:/MEDICUS 3/data/medicus.FDB"
    "?charset=WIN1250")

# Create engine
engine = create_engine(connection_string)

# Read data
query = "SELECT idpac, rodcis FROM kar"
df = pd.read_sql(query, engine)

# Close connection (SQLAlchemy handles this automatically in most cases)
engine.dispose()

print(df.head())

with pd.ExcelWriter(reportname, engine='openpyxl') as writer:
    df.to_excel(writer, index=False)
    # Access the worksheet
    worksheet = writer.sheets['Sheet1']

    # Apply text format to specific columns
    for col in ['B']:  # Columns A and C (ID and ProductCode)
        for cell in worksheet[col]:
            cell.number_format = '@'  # Excel text format