notebook
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
import os
|
||||
from Functions import get_path_ciselniky
|
||||
|
||||
cesta=get_path_ciselniky()
|
||||
print(cesta)
|
||||
|
||||
files = [f for f in os.listdir(cesta) if os.path.isfile(os.path.join(cesta, f))]
|
||||
print(files)
|
||||
@@ -1,13 +0,0 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
cesta=r"\\tower\library"
|
||||
|
||||
for dirpath,dirnames,filenames in os.walk(cesta):
|
||||
|
||||
for ddir in dirnames:
|
||||
# print(ddir)
|
||||
p=Path(dirpath, ddir)
|
||||
if not any(p.iterdir()):
|
||||
print(f"Directory is empty {p}")
|
||||
p.rmdir()
|
||||
print(f"Directory was deleted {p}")
|
||||
@@ -5,6 +5,15 @@ import pymysql
|
||||
from pymysql.cursors import DictCursor
|
||||
import socket
|
||||
|
||||
def get_path_ciselniky():
|
||||
hostname = socket.gethostname().strip()
|
||||
if hostname in ("NTBVBHP470G10", "Z230"):
|
||||
return r"u:\Dropbox\!!!Days\Downloads Z230\Pracuji_na\Import"
|
||||
elif hostname == "SESTRA":
|
||||
return r"z:\Dropbox\!!!Days\Downloads Z230\Pracuji_na\Import"
|
||||
else:
|
||||
print(f"Unknown host: {hostname}")
|
||||
return None
|
||||
|
||||
def get_mysql_connection(cursor_mode=None):
|
||||
"""
|
||||
|
||||
140
ReportDavky/01ReportDavky.py
Normal file
140
ReportDavky/01ReportDavky.py
Normal file
@@ -0,0 +1,140 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from pathlib import Path
|
||||
import time
|
||||
import fdb
|
||||
import pandas as pd
|
||||
from openpyxl import load_workbook
|
||||
from openpyxl.worksheet.table import Table, TableStyleInfo
|
||||
from openpyxl.styles import Font, PatternFill, Alignment
|
||||
from openpyxl.utils import get_column_letter
|
||||
|
||||
# ================== Výstupní cesta ==================
|
||||
BASE_DIR = Path(r"..") # uprav dle potřeby
|
||||
timestamp = time.strftime("%Y-%m-%d %H-%M-%S")
|
||||
xlsx_name = f"{timestamp} Davky report.xlsx"
|
||||
xlsx_path = BASE_DIR / xlsx_name
|
||||
|
||||
# ================== Připojení k DB ==================
|
||||
con = fdb.connect(
|
||||
host='192.168.1.4',
|
||||
database=r'z:\\MEDICUS 3\\data\\medicus.FDB',
|
||||
user='sysdba',
|
||||
password='masterkey',
|
||||
charset='WIN1250'
|
||||
)
|
||||
|
||||
#Toto je dotaz na řádky DohladH pro rok 2025
|
||||
sql = """
|
||||
SELECT
|
||||
idhlav,
|
||||
poj,
|
||||
icz,
|
||||
cisdokl,
|
||||
rodcis,
|
||||
hdgn,
|
||||
leciva,
|
||||
porcis,
|
||||
davka,
|
||||
oprav,
|
||||
rok,
|
||||
typ
|
||||
from DOKLADH
|
||||
where rok=2025
|
||||
order by idhlav desc;
|
||||
"""
|
||||
#A toto je dotaz na řádky DohladD (details) pro rok 2025
|
||||
sql2="""select id, idhlav, rodcis, datose, kod, pocvyk, ddgn, body,uct,zprac from dokladd where extract(year from datose)=2025
|
||||
order by idhlav desc"""
|
||||
|
||||
# ================== Načtení do DataFrame ==================
|
||||
df = pd.read_sql(sql, con)
|
||||
df2= pd.read_sql(sql2, con)
|
||||
con.close()
|
||||
|
||||
# Textové sloupce jako string
|
||||
for col in ["Kód MZ", "Šarže", "Rodné číslo", "Látka", "Název", "Příjmení", "Jméno", "LECD kódy","Výkony"]:
|
||||
if col in df.columns:
|
||||
df[col] = df[col].astype("string")
|
||||
|
||||
# Převod dat na datetime
|
||||
for dcol in ["Datum očkování", "Expirace"]:
|
||||
if dcol in df.columns:
|
||||
df[dcol] = pd.to_datetime(df[dcol], errors="coerce")
|
||||
|
||||
# ================== Uložení do Excelu ==================
|
||||
with pd.ExcelWriter(xlsx_path, engine="openpyxl") as writer:
|
||||
df.to_excel(writer, index=False, sheet_name="DávkyHeader")
|
||||
df2.to_excel(writer, index=False, sheet_name="DávkyDetails")
|
||||
|
||||
# ================== Formátování ==================
|
||||
wb = load_workbook(xlsx_path)
|
||||
|
||||
def autosize_columns(ws):
|
||||
for col_idx in range(1, ws.max_column + 1):
|
||||
col_letter = get_column_letter(col_idx)
|
||||
max_len = 0
|
||||
for cell in ws[col_letter]:
|
||||
val = "" if cell.value is None else str(cell.value)
|
||||
if len(val) > max_len:
|
||||
max_len = len(val)
|
||||
ws.column_dimensions[col_letter].width = min(max(12, max_len + 2), 60)
|
||||
def style_table(ws, table_name: str):
|
||||
max_row = ws.max_row
|
||||
max_col = ws.max_column
|
||||
if max_col == 0:
|
||||
return
|
||||
|
||||
# Hlavička vzhled
|
||||
header_fill = PatternFill("solid", fgColor="D9E1F2")
|
||||
for cell in ws[1]:
|
||||
cell.font = Font(bold=True)
|
||||
cell.fill = header_fill
|
||||
cell.alignment = Alignment(vertical="center")
|
||||
|
||||
# Freeze top row
|
||||
ws.freeze_panes = "A2"
|
||||
|
||||
# Tabulka jen pokud jsou data
|
||||
if max_row < 2:
|
||||
autosize_columns(ws)
|
||||
return
|
||||
|
||||
ref = f"A1:{get_column_letter(max_col)}{max_row}"
|
||||
tbl = Table(displayName=table_name, ref=ref)
|
||||
tbl.tableStyleInfo = TableStyleInfo(
|
||||
name="TableStyleMedium9", showRowStripes=True, showColumnStripes=False
|
||||
)
|
||||
ws.add_table(tbl)
|
||||
autosize_columns(ws)
|
||||
def format_dates(ws, columns_names):
|
||||
header = [c.value for c in ws[1]]
|
||||
date_cols = [header.index(name) + 1 for name in columns_names if name in header]
|
||||
for col_idx in date_cols:
|
||||
for row in ws.iter_rows(min_row=2, min_col=col_idx, max_col=col_idx, max_row=ws.max_row):
|
||||
row[0].number_format = "DD.MM.YYYY"
|
||||
def center_columns(ws,columns_names):
|
||||
# Define centered alignment
|
||||
center_alignment = Alignment(horizontal="center", vertical="center")
|
||||
if isinstance(columns_names,list):
|
||||
pass
|
||||
elif isinstance(columns_names,str):
|
||||
if columns_names=="ALL":
|
||||
for col_idx in range(1, ws.max_column + 1):
|
||||
col_letter = get_column_letter(col_idx)
|
||||
max_len = 0
|
||||
for cell in ws[col_letter]:
|
||||
cell.alignment = center_alignment
|
||||
|
||||
wsdavkyheader = wb["DávkyHeader"]
|
||||
wsdavkydetails = wb["DávkyDetails"]
|
||||
style_table(wsdavkyheader, "dávkyheader")
|
||||
style_table(wsdavkydetails, "dávkydetails")
|
||||
# format_dates(ws_main, ["Datum očkování", "Expirace"])
|
||||
center_columns(wsdavkyheader,"ALL")
|
||||
center_columns(wsdavkydetails,"ALL")
|
||||
|
||||
wb.save(xlsx_path)
|
||||
|
||||
print(f"Hotovo. Uloženo do: {xlsx_path.resolve()}")
|
||||
114
StrukturaMedicus/Strukturamedicus.py
Normal file
114
StrukturaMedicus/Strukturamedicus.py
Normal file
@@ -0,0 +1,114 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import fdb
|
||||
from datetime import datetime
|
||||
|
||||
# === CONFIG ===
|
||||
DB_PATH = r"U:\Medicus 3\data\MEDICUS.FDB"
|
||||
USER = "sysdba"
|
||||
PASSWORD = "masterkey"
|
||||
|
||||
# output file (timestamped)
|
||||
OUTFILE = f"firebird_schema_{datetime.now():%Y%m%d_%H%M%S}.txt"
|
||||
|
||||
# === CONNECT ===
|
||||
con = fdb.connect(
|
||||
dsn=DB_PATH,
|
||||
user=USER,
|
||||
password=PASSWORD,
|
||||
charset='WIN1250'
|
||||
)
|
||||
cur = con.cursor()
|
||||
|
||||
with open(OUTFILE, "w", encoding="utf-8") as f:
|
||||
f.write(f"Firebird schema overview for: {DB_PATH}\n\n")
|
||||
|
||||
# === GET ALL USER TABLES ===
|
||||
cur.execute("""
|
||||
SELECT r.rdb$relation_name
|
||||
FROM rdb$relations r
|
||||
WHERE r.rdb$system_flag = 0
|
||||
AND r.rdb$view_blr IS NULL
|
||||
ORDER BY r.rdb$relation_name
|
||||
""")
|
||||
tables = [t[0].strip() for t in cur.fetchall()]
|
||||
|
||||
for table in tables:
|
||||
f.write("=" * 80 + "\n")
|
||||
f.write(f"TABLE: {table}\n")
|
||||
f.write("-" * 80 + "\n")
|
||||
|
||||
# --- COLUMNS ---
|
||||
cur.execute(f"""
|
||||
SELECT
|
||||
rf.rdb$field_name,
|
||||
f.rdb$field_type,
|
||||
f.rdb$field_sub_type,
|
||||
f.rdb$field_length,
|
||||
f.rdb$field_scale,
|
||||
coalesce(rf.rdb$null_flag, 0),
|
||||
rf.rdb$default_source,
|
||||
rf.rdb$description
|
||||
FROM rdb$relation_fields rf
|
||||
JOIN rdb$fields f ON rf.rdb$field_source = f.rdb$field_name
|
||||
WHERE rf.rdb$relation_name = '{table}'
|
||||
ORDER BY rf.rdb$field_position
|
||||
""")
|
||||
|
||||
f.write("Columns:\n")
|
||||
for row in cur.fetchall():
|
||||
name = row[0].strip()
|
||||
dtype = row[1]
|
||||
subtype = row[2]
|
||||
length = row[3]
|
||||
scale = row[4]
|
||||
notnull = bool(row[5])
|
||||
default = (row[6] or "").strip() if row[6] else ""
|
||||
desc = (row[7] or "").strip() if row[7] else ""
|
||||
f.write(f" - {name:<20} type={dtype} sub={subtype} len={length} "
|
||||
f"scale={scale} notnull={notnull} default={default} desc={desc}\n")
|
||||
|
||||
# --- INDEXES ---
|
||||
cur.execute(f"""
|
||||
SELECT
|
||||
i.rdb$index_name,
|
||||
i.rdb$unique_flag,
|
||||
i.rdb$index_type,
|
||||
s.rdb$field_name
|
||||
FROM rdb$indices i
|
||||
JOIN rdb$index_segments s ON i.rdb$index_name = s.rdb$index_name
|
||||
WHERE i.rdb$relation_name = '{table}'
|
||||
ORDER BY i.rdb$index_name, s.rdb$field_position
|
||||
""")
|
||||
idx = cur.fetchall()
|
||||
if idx:
|
||||
f.write("Indexes:\n")
|
||||
for row in idx:
|
||||
f.write(f" - {row[0].strip():<30} field={row[3].strip()} unique={bool(row[1])} type={row[2]}\n")
|
||||
else:
|
||||
f.write("Indexes: none\n")
|
||||
|
||||
# --- PRIMARY & FOREIGN KEYS ---
|
||||
cur.execute(f"""
|
||||
SELECT
|
||||
rc.rdb$constraint_name,
|
||||
rc.rdb$constraint_type,
|
||||
seg.rdb$field_name
|
||||
FROM rdb$relation_constraints rc
|
||||
JOIN rdb$index_segments seg ON rc.rdb$index_name = seg.rdb$index_name
|
||||
WHERE rc.rdb$relation_name = '{table}'
|
||||
ORDER BY rc.rdb$constraint_name
|
||||
""")
|
||||
cons = cur.fetchall()
|
||||
if cons:
|
||||
f.write("Constraints:\n")
|
||||
for row in cons:
|
||||
f.write(f" - {row[1].strip():<15} {row[0].strip()} → {row[2].strip()}\n")
|
||||
else:
|
||||
f.write("Constraints: none\n")
|
||||
|
||||
f.write("\n")
|
||||
|
||||
con.close()
|
||||
print(f"✅ Schema saved to: {OUTFILE}")
|
||||
22200
StrukturaMedicus/firebird_schema_20251012_080542.txt
Normal file
22200
StrukturaMedicus/firebird_schema_20251012_080542.txt
Normal file
File diff suppressed because it is too large
Load Diff
142
pomoc.py
Normal file
142
pomoc.py
Normal file
@@ -0,0 +1,142 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from pathlib import Path
|
||||
import time
|
||||
import fdb
|
||||
import pandas as pd
|
||||
from openpyxl import load_workbook
|
||||
from openpyxl.worksheet.table import Table, TableStyleInfo
|
||||
from openpyxl.styles import Font, PatternFill, Alignment
|
||||
from openpyxl.utils import get_column_letter
|
||||
|
||||
# ================== Výstupní cesta ==================
|
||||
BASE_DIR = Path(r"..") # uprav dle potřeby
|
||||
timestamp = time.strftime("%Y-%m-%d %H-%M-%S")
|
||||
xlsx_name = f"Pacienti očkování {timestamp}.xlsx"
|
||||
xlsx_path = BASE_DIR / xlsx_name
|
||||
|
||||
# ================== Připojení k DB ==================
|
||||
con = fdb.connect(
|
||||
host='192.168.1.4',
|
||||
database=r'z:\\MEDICUS 3\\data\\medicus.FDB',
|
||||
user='sysdba',
|
||||
password='masterkey',
|
||||
charset='WIN1250'
|
||||
)
|
||||
|
||||
# ================== SQL dotaz ==================
|
||||
# Pozn.: CAST(ockzaz.datum AS DATE) pro přesné párování na lecd.datose (DATE).
|
||||
sql = """
|
||||
SELECT
|
||||
kar.rodcis AS "Rodné číslo",
|
||||
kar.prijmeni AS "Příjmení",
|
||||
kar.jmeno AS "Jméno",
|
||||
ockzaz.datum AS "Datum očkování",
|
||||
ockzaz.kodmz AS "Kód MZ",
|
||||
ockzaz.poznamka AS "Šarže",
|
||||
ockzaz.latka AS "Látka",
|
||||
ockzaz.nazev AS "Název",
|
||||
ockzaz.expire AS "Expirace",
|
||||
|
||||
/* --- VŠECHNY KÓDY Z TABULKY LECD PRO TOHO PACIENTA A TENTÝŽ DEN --- */
|
||||
(
|
||||
SELECT LIST(l.kod, ', ')
|
||||
FROM lecd l
|
||||
WHERE l.rodcis = kar.rodcis
|
||||
AND l.datose = CAST(ockzaz.datum AS DATE)
|
||||
) AS "LECD kódy (ten den)",
|
||||
/* --- VŠECHNY KÓDY Z TABULKY vykonu dokladd PRO TOHO PACIENTA A TENTÝŽ DEN --- */
|
||||
(
|
||||
SELECT LIST(d.kod, ', ')
|
||||
FROM dokladd d
|
||||
WHERE d.rodcis = kar.rodcis
|
||||
AND d.datose = CAST(ockzaz.datum AS DATE)
|
||||
) AS "Vykony (ten den)"
|
||||
|
||||
FROM registr
|
||||
JOIN kar ON registr.idpac = kar.idpac
|
||||
JOIN ockzaz ON registr.idpac = ockzaz.idpac
|
||||
WHERE
|
||||
registr.datum_zruseni IS NULL
|
||||
AND kar.vyrazen <> 'A'
|
||||
AND kar.rodcis IS NOT NULL
|
||||
AND idicp <> 0
|
||||
and EXTRACT(YEAR FROM ockzaz.datum)=2025
|
||||
ORDER BY ockzaz.datum DESC
|
||||
"""
|
||||
#and nazev containing 'chřipka'
|
||||
# ================== Načtení do DataFrame ==================
|
||||
df = pd.read_sql(sql, con)
|
||||
con.close()
|
||||
|
||||
# Textové sloupce jako string
|
||||
for col in ["Kód MZ", "Šarže", "Rodné číslo", "Látka", "Název", "Příjmení", "Jméno", "LECD kódy","Výkony"]:
|
||||
if col in df.columns:
|
||||
df[col] = df[col].astype("string")
|
||||
|
||||
# Převod dat na datetime
|
||||
for dcol in ["Datum očkování", "Expirace"]:
|
||||
if dcol in df.columns:
|
||||
df[dcol] = pd.to_datetime(df[dcol], errors="coerce")
|
||||
|
||||
# ================== Uložení do Excelu ==================
|
||||
with pd.ExcelWriter(xlsx_path, engine="openpyxl") as writer:
|
||||
df.to_excel(writer, index=False, sheet_name="Očkování")
|
||||
|
||||
# ================== Formátování ==================
|
||||
wb = load_workbook(xlsx_path)
|
||||
|
||||
def autosize_columns(ws):
|
||||
for col_idx in range(1, ws.max_column + 1):
|
||||
col_letter = get_column_letter(col_idx)
|
||||
max_len = 0
|
||||
for cell in ws[col_letter]:
|
||||
val = "" if cell.value is None else str(cell.value)
|
||||
if len(val) > max_len:
|
||||
max_len = len(val)
|
||||
ws.column_dimensions[col_letter].width = min(max(12, max_len + 2), 60)
|
||||
|
||||
def style_table(ws, table_name: str):
|
||||
max_row = ws.max_row
|
||||
max_col = ws.max_column
|
||||
if max_col == 0:
|
||||
return
|
||||
|
||||
# Hlavička vzhled
|
||||
header_fill = PatternFill("solid", fgColor="D9E1F2")
|
||||
for cell in ws[1]:
|
||||
cell.font = Font(bold=True)
|
||||
cell.fill = header_fill
|
||||
cell.alignment = Alignment(vertical="center")
|
||||
|
||||
# Freeze top row
|
||||
ws.freeze_panes = "A2"
|
||||
|
||||
# Tabulka jen pokud jsou data
|
||||
if max_row < 2:
|
||||
autosize_columns(ws)
|
||||
return
|
||||
|
||||
ref = f"A1:{get_column_letter(max_col)}{max_row}"
|
||||
tbl = Table(displayName=table_name, ref=ref)
|
||||
tbl.tableStyleInfo = TableStyleInfo(
|
||||
name="TableStyleMedium9", showRowStripes=True, showColumnStripes=False
|
||||
)
|
||||
ws.add_table(tbl)
|
||||
autosize_columns(ws)
|
||||
|
||||
def format_dates(ws, columns_names):
|
||||
header = [c.value for c in ws[1]]
|
||||
date_cols = [header.index(name) + 1 for name in columns_names if name in header]
|
||||
for col_idx in date_cols:
|
||||
for row in ws.iter_rows(min_row=2, min_col=col_idx, max_col=col_idx, max_row=ws.max_row):
|
||||
row[0].number_format = "DD.MM.YYYY"
|
||||
|
||||
ws_main = wb["Očkování"]
|
||||
style_table(ws_main, "Ockovani")
|
||||
format_dates(ws_main, ["Datum očkování", "Expirace"])
|
||||
|
||||
wb.save(xlsx_path)
|
||||
|
||||
print(f"Hotovo. Uloženo do: {xlsx_path.resolve()}")
|
||||
Reference in New Issue
Block a user