Import specific range of Excel in Python

Hello friends! Today we’ll be learning how to import specific range of excel data in Python. Easy to follow code.

Import Libraries

from openpyxl import load_workbook
import docx
import os

Import Data

Following code will read the excel file with sheet and the range A1 to C5.

wb = load_workbook(filename='TableRead.xlsx', read_only=True)
ws = wb['Sheet1']
# Read the cell values into a list of lists
data_rows = []
for row in ws['A1':'C5']:
data_cols = []
for cell in row:
data_cols.append(cell.value)
data_rows.append(data_cols)
# Transform into dataframe
import pandas as pd
df = pd.DataFrame(data_rows)

Keep visiting Analytics Tuts for more tutorials.

Thanks for reading! Comment your suggestions and queries


Leave a Reply

Your email address will not be published. Required fields are marked *