import os
import re
import fitz  # PyMuPDF
import docx
import xlrd
import openpyxl
import olefile
from io import BytesIO
from win32com import client as win32

# Taranacak klasör yolu
klasor_yolu = r"\\79.123.210.55\dosyalar"

# TCKN regex (boşluk / tireli formatlar dahil)
tckn_regex = re.compile(r'[1-9]\d{2}[- ]?\d{3}[- ]?\d{2}[- ]?\d{2}')

bulunan_dosyalar = []

def pdf_oku(dosya_yolu):
    try:
        text = ""
        with fitz.open(dosya_yolu) as doc:
            for page in doc:
                text += page.get_text()
        return text
    except Exception as e:
        print(f"[PDF HATA] {dosya_yolu} -> {e}")
        return ""

def docx_oku(dosya_yolu):
    try:
        doc = docx.Document(dosya_yolu)
        return "\n".join([p.text for p in doc.paragraphs])
    except Exception as e:
        print(f"[DOCX HATA] {dosya_yolu} -> {e}")
        return ""

def xlsx_oku(dosya_yolu):
    try:
        wb = openpyxl.load_workbook(dosya_yolu, data_only=True)
        text = ""
        for sheet in wb.sheetnames:
            ws = wb[sheet]
            for row in ws.iter_rows():
                for cell in row:
                    if cell.value:
                        text += str(cell.value) + " "
        return text
    except Exception as e:
        print(f"[XLSX HATA] {dosya_yolu} -> {e}")
        return ""

def xls_oku(dosya_yolu):
    try:
        wb = xlrd.open_workbook(dosya_yolu)
        text = ""
        for sheet in wb.sheets():
            for row_idx in range(sheet.nrows):
                row_values = sheet.row_values(row_idx)
                text += " ".join([str(v) for v in row_values]) + " "
        return text
    except Exception as e:
        print(f"[XLS HATA] {dosya_yolu} -> {e}")
        return ""

def doc_oku(dosya_yolu):
    try:
        word = win32.Dispatch("Word.Application")
        word.Visible = False
        doc = word.Documents.Open(dosya_yolu)
        text = doc.Content.Text
        doc.Close(False)
        word.Quit()
        return text
    except Exception as e:
        print(f"[DOC HATA] {dosya_yolu} -> {e}")
        return ""

# Klasörü tara
for root, dirs, files in os.walk(klasor_yolu):
    for file in files:
        dosya_yolu = os.path.join(root, file)
        print(f"Taranıyor: {dosya_yolu}")

        icerik = ""
        if file.lower().endswith(".pdf"):
            icerik = pdf_oku(dosya_yolu)
        elif file.lower().endswith(".docx"):
            icerik = docx_oku(dosya_yolu)
        elif file.lower().endswith(".xlsx"):
            icerik = xlsx_oku(dosya_yolu)
        elif file.lower().endswith(".xls"):
            icerik = xls_oku(dosya_yolu)
        elif file.lower().endswith(".doc"):
            icerik = doc_oku(dosya_yolu)

        if icerik and tckn_regex.search(icerik):
            bulunan_dosyalar.append(dosya_yolu)

print("\n--- TCKN BULUNAN DOSYALAR ---")
if bulunan_dosyalar:
    for dosya in bulunan_dosyalar:
        print(dosya)
else:
    print("Hiçbir dosyada TCKN bulunamadı.")
