Чтобы открыть файл Excel в Python и получить доступ к его строкам и столбцам, вы можете использовать различные библиотеки и методы. Вот некоторые часто используемые подходы:
-
Использование библиотеки
pandas:import pandas as pd # Open the Excel file df = pd.read_excel('filename.xlsx') # Access rows and columns # Access a specific column by name column_data = df['column_name'] # Access a specific row by index row_data = df.iloc[row_index] # Access a specific cell by row and column cell_data = df.iloc[row_index, column_index] -
Использование библиотеки
xlrd:import xlrd # Open the Excel file workbook = xlrd.open_workbook('filename.xlsx') # Access a specific sheet sheet = workbook.sheet_by_index(0) # Assuming the first sheet # Access rows and columns # Access a specific column by index column_data = sheet.col_values(column_index) # Access a specific row by index row_data = sheet.row_values(row_index) # Access a specific cell by row and column cell_data = sheet.cell_value(row_index, column_index) -
Использование библиотеки
openpyxl:import openpyxl # Open the Excel file workbook = openpyxl.load_workbook('filename.xlsx') # Access a specific sheet sheet = workbook['sheet_name'] # Replace 'sheet_name' with the actual sheet name # Access rows and columns # Access a specific column by letter column_data = [cell.value for cell in sheet['column_letter']] # Access a specific row by index row_data = [cell.value for cell in sheet[row_index]] # Access a specific cell by row and column cell_data = sheet.cell(row=row_index, column=column_index).value
Не забудьте заменить 'filename.xlsx'фактическим путем к файлу Excel и соответствующим образом откорректировать индексы листа и столбца/строки.