Tuesday, 26 March 2024

python captureScree

 import datetime

import os
from datetime import datetime
import keyboard
import pygetwindow
import pyautogui
from PIL import Image
from azure.ai.formrecognizer import DocumentAnalysisClient
from azure.core.credentials import AzureKeyCredential
import pyperclip
import mss
# import yaml
#
# def read_credentials(file_path):
#     with open(file_path, 'r') as file:
#         credentials = yaml.safe_load(file)
#         return credentials
#
# # Path to your credentials file
# credentials_file = 'credentials.yaml'
#
# # Read credentials
# credentials = read_credentials(credentials_file)
# key = credentials.get('azure_key', '')
# endpoint = credentials.get('azure_endpoint', '')
key = "xxxxxxxxxxxxxxxxxxxxxe0"
endpoint = "https://xxxxxxxxxxxxxxxxxxxxxe.cognitiveservices.azure.com/"

def screenshot(path):

    window = pygetwindow.getActiveWindow()
    #left, top = window.topleft
    #right, bottom = window.bottomright
    bbox = {
        "top": window.top,
        "left": window.left+10,
        "width": window.width-20,
        "height": window.height-10
    }
    current_time = datetime.now().strftime("%Y%m%d_%H%M%S")
    title = str(window.title).replace("\\", "").replace(":", "").replace(" ", "")
    image_path = path + f"screen_{title}_{current_time}.png"
    #pyautogui.screenshot(image_path)
    #im = Image.open(image_path)
    #im = im.crop((left+10, top, right-10, bottom-10))
    #im.save(image_path)
    with mss.mss() as sct:
        # Capture the screen within the bounding box
        sct_img = sct.grab(bbox)

        # Save to the picture file
        mss.tools.to_png(sct_img.rgb, sct_img.size, output=image_path)
    return image_path


# formatting function
def format_polygon(polygon):
    if not polygon:
        return "N/A"
    return ", ".join(["[{}, {}]".format(p.x, p.y) for p in polygon])


def find_screenshot_type(content):
    '''
    Looks for certain keywords to detect document type
    '''
    if 'AWD - History' in content:
        return 'AWD - History'
    return None


def generate_table(table):
    '''
    Convert result table to a 2D list considering column_index, column_span, row_index, and row_span for each cell.
    '''
    column_count = table.column_count
    row_count = table.row_count
    cells = table.cells

    # Initialize the 2-dimensional list with None to represent empty cells initially
    table1 = [[None for _ in range(column_count)] for _ in range(row_count)]

    for cell in cells:
        # Use the cell's row_index and column_index to position it correctly
        row_idx = cell.row_index
        col_idx = cell.column_index

        # Fill the cell content into the table, considering column_span and row_span
        # Here, we only fill the top-left cell of a spanned area with content,
        # and leave the rest as None or some placeholder to indicate it's part of a span
        # This can be adjusted based on how you want to handle spans
        if table1[row_idx][col_idx] is None:  # Check if cell is not already filled (useful for spans)
            table1[row_idx][col_idx] = cell.content
            # If there's a span, you might want to mark the spanned cells in some way
            # For simplicity, this code does not populate all spanned cells with content
            # but you could modify it to suit your needs (e.g., fill all spanned cells with the same content)

    return table1

def find_processed_and_related_rows(table):
    # Find rows with 'PROCESSED' in column 6 and capture the value of column 3
    processed_ids = set()
    for row in table:
        if 'PROCESSED' in row:
            processed_ids.add(row[2])

    # Find all rows with the captured column 3 values or the next row has an empty string or None for column 3
    related_rows = []
    for i, row in enumerate(table):
        # Check if the current row's column 3 matches any captured user ID
        if row[2] in processed_ids:
            related_rows.append(row)
            # Check the next row for an empty string or None in column 3
            if i + 1 < len(table) and (table[i + 1][2] == '' or table[i + 1][2] is None):
                related_rows.append(table[i + 1])

    return processed_ids, related_rows


def find_qpassed_and_related_rows(table):
    # Find rows with 'PROCESSED' in column 6 and capture the value of column 3
    qpassed_ids = set()
    for row in table:
        if ('QCPASSED' in row):
            qpassed_ids.add(row[2])

    # Find all rows with the captured column 3 values or the next row has an empty string or None for column 3
    related_rows = []
    for i, row in enumerate(table):
        # Check if the current row's column 3 matches any captured user ID
        if row[2] in qpassed_ids:
            related_rows.append(row)

    return qpassed_ids, related_rows

def find_qwpassed_and_related_rows(table):
    # Find rows with 'PROCESSED' in column 6 and capture the value of column 3
    qpassed_ids = set()
    for row in table:
        if ('QWPASSED' in row):
            qpassed_ids.add(row[2])

    # Find all rows with the captured column 3 values or the next row has an empty string or None for column 3
    related_rows = []
    for i, row in enumerate(table):
        # Check if the current row's column 3 matches any captured user ID
        if row[2] in qpassed_ids:
            related_rows.append(row)

    return qpassed_ids, related_rows

def find_qcreworked_and_related_rows(table):
    # Find rows with 'PROCESSED' in column 6 and capture the value of column 3
    qpassed_ids = set()
    for row in table:
        if ('QCREWORKED' in row):
            qpassed_ids.add(row[2])

    # Find all rows with the captured column 3 values or the next row has an empty string or None for column 3
    related_rows = []
    for i, row in enumerate(table):
        # Check if the current row's column 3 matches any captured user ID
        if row[2] in qpassed_ids:
            related_rows.append(row)

    return qpassed_ids, related_rows

def format_table_for_printing(table):
    # Determine the maximum width of each column
    col_widths = [max(len(str(cell)) for cell in column) for column in zip(*table)]

    # Create a formatted string for printing
    table_str = ""
    for row in table:
        row_str = " | ".join(str(cell).ljust(col_widths[i]) for i, cell in enumerate(row))
        table_str += row_str + "\n" + "-" * (sum(col_widths) + len(col_widths) * 3 - 1) + "\n"

    return table_str


def analyze_read(path):
    # sample document
    print(f"----\nAnalyzing {path} ...\n----")
    document = open(path, "rb")

    document_analysis_client = DocumentAnalysisClient(
        endpoint=endpoint, credential=AzureKeyCredential(key)
    )

    poller = document_analysis_client.begin_analyze_document(
        #"prebuilt-read", document
        "prebuilt-document", document
    )

    result = poller.result()

    doc_type = find_screenshot_type(result.content)
    if doc_type == "AWD - History":
        content = "Document type: awd-history\n"
        try:
            table = generate_table(result.tables[0])
            ids, rows = find_processed_and_related_rows(table)
            if ids and rows:
                content += "----------------------------------------\n"
                content += "PROCESSED OK\n"
                content += f"ids: {ids}\n"
                for r in rows:
                    if r[len(r) - 1]:
                        content += f"{r[len(r) - 1]}\n"
                content += "----------------------------------------\n"
            ids, rows = find_qpassed_and_related_rows(table)
            if ids and rows:
                content += "----------------------------------------\n"
                content += "QCPASSED OK\n"
                content += f"ids: {ids}\n"
                for r in rows:
                    if r[len(r) - 1]:
                        content += f"{r[len(r) - 1]}\n"
                content += "----------------------------------------\n"
            ids, rows = find_qwpassed_and_related_rows(table)
            if ids and rows:
                content += "----------------------------------------\n"
                content += "QWPASSED OK\n"
                content += f"ids: {ids}\n"
                for r in rows:
                    if r[len(r) - 1]:
                        content += f"{r[len(r) - 1]}\n"
                content += "----------------------------------------\n"
            ids, rows = find_qcreworked_and_related_rows(table)
            if ids and rows:
                content += "----------------------------------------\n"
                content += "QCREWORKED OK\n"
                content += f"ids: {ids}\n"
                for r in rows:
                    if r[len(r) - 1]:
                        content += f"{r[len(r) - 1]}\n"
                content += "----------------------------------------\n"
            content += format_table_for_printing(table)
        except:
            print("Cannot find table in awd-history switching to regular output")
            content = result.content
    else:
        content = result.content

    # Copying the text to the clipboard
    pyperclip.copy(content)

    print("Document contains content: \n", content)
    print("----------------------------------------")


def create_folder():
    folder = "C:\\Temp\\"

    # Check if the folder exists
    if not os.path.exists(folder):
        # If it doesn't exist, create it
        os.makedirs(folder)
        print(f"Folder '{folder}' was created.")
    else:
        print(f"Folder '{folder}' already exists.")
    return folder


# Function to be called when Ctrl+Shift+S is pressed
def on_triggered():
    path = create_folder()
    print(path)
    print("Taking screenshot...")
    screenshot_path = screenshot(path)
    if screenshot_path:
        print(f"Screenshot saved at {screenshot_path}")
        analyze_read(screenshot_path)
    else:
        print("Failed to take screenshot.")

# Listen for the Ctrl+Shift+X combination
keyboard.add_hotkey('ctrl+shift+x', on_triggered)

print("Press Ctrl+Shift+X to take a screenshot of the active window.")
print("Screenshot goes to C:\\Temp")
print("Captured text goes in clipboard. Use CTRL+V to paste.")
print("Press ESC to exit.")
keyboard.wait('esc')  # Use 'esc' key to stop the program

No comments:

Post a Comment

Connect broadband