Forum Discussion
scot1967
Champion Level 3
2 hours agoHello Afreen, 👋🏼
I am not much on Python but this is a JavaScipt version that will take the workbook object and Sheet Name specification and return the sheet object.
This link should explain a lot.
https://support.smartbear.com/testcomplete/docs/reference/program-objects/excelsheet/index.html
/* JavaScript */
GetExcelFileSheetObject(objExcelWorkBook, strSheetName) {
try {
let objSheet = objExcelWorkBook.Sheets.Item(strSheetName);
return objSheet;
}
catch(ex) {
Log.Error("GetExcelFileSheetObject: " + ex.stack);
}
}
AI 🤖 translates it as this...
Python Version (openpyxl):
def get_excel_file_sheet_object(workbook, sheet_name):
try:
sheet = workbook[sheet_name]
return sheet
except Exception as ex:
print(f"GetExcelFileSheetObject: {ex}")
Usage Example:
from openpyxl import load_workbook
workbook = load_workbook("example.xlsx")
sheet = get_excel_file_sheet_object(workbook, "Sheet1")
If You Want Logging Instead of print:
import logging
def get_excel_file_sheet_object(workbook, sheet_name):
try:
return workbook[sheet_name]
except Exception as ex:
logging.error("GetExcelFileSheetObject: %s", ex)
return None
I hope this gets you close enough to get going!
... If you find my posts helpful drop me a Like👍 Be sure to mark the post as the Solution✅ when you get one to help others out and to credit the one who helped you. 😎