سؤال

I have a program that reads data from a worksheet in an already opened excel file. However if the wrong file/worksheet is open the data will be corrupted.

I initiate excel using the following. But I cannot find an object of xcel that contains filename or worksheet name.

import win32com.client
xcel = win32com.client.Dispatch("Excel.Application")

What I am looking for is a way to read the filename of the excel file and the name of the worksheet.

هل كانت مفيدة؟

المحلول

There is no filename if you open a new excel application ... You open a new file this way excel.Workbooks.Open(Filename,ReadOnly)

And this is for checking sheet name

import win32com.client
excel = win32com.client.Dispatch('Excel.Application')

wb = excel.Workbooks.Add()
ws = wb.Sheets(1)

if ws.Name!="TheSheetImLookingFor":
    raise ValueError(ws.Name)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top