Can't copy a cell information using Win32com library

45 Views Asked by At

So basically I'm creating a automation that copy a information from a cell and save it in to a var so i can paste in to a app like notepad or wathever

import win32com.client as win32
import pyautogui
import global_variables


def copiar():
    try:
        excel = win32.GetActiveObject("Excel.Application")
    except:
        print("Excel not found.")
        return

    # Obter o nome da planilha ativa
    workbook_name = excel.ActiveWorkbook.Name

    # Se a planilha ativa não for a desejada, você pode abrir a planilha usando o seu caminho
    if workbook_name != 'CONEMB_ZLE.xlsx':
        # Exibe uma mensagem de aviso
        print("Excel not found, open.")
        return

    # Tornar o Excel visível
    excel.Visible = True

    # Selecionar a planilha ativa
    sheet = excel.ActiveSheet

    # Obter o valor da célula selecionada
    global_variables.valor_global = sheet.Application.Selection.Text

    # Imprimir o valor
    #print(global_variables.valor_global)

copiar()

It was working until today, I don't know why its doesnt recognize my Excel archive anymore, it prints excel not found but the Excel is opened with the same archive

I use it this way because i don't want to have to put the archive location I'm 3 hours tryng to fix that and i use my Excel archives on my Onedrive.

Here is the error message:

Traceback (most recent call last): File "C:\Users\bissojhe\PycharmProjects\pythonProject\.venv\py32.py", line 36, in <module> copiar() File "C:\Users\bissojhe\PycharmProjects\pythonProject\.venv\py32.py", line 8, in copiar excel = win32.GetActiveObject("Excel.Application") ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\bissojhe\PycharmProjects\pythonProject\.venv\Lib\site-packages\win32com\client_init_.py", line 96, in GetActiveObject return __WrapDispatch(dispatch, Class, resultCLSID=resultCLSID, –
0

There are 0 best solutions below