Read excel file python openpyxl

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Openpyxl is a Python library for reading and writing Excel (with extension xlsx/xlsm/xltx/xltm) files. The openpyxl module allows Python program to read and modify Excel files.
    For example, users might have to go through thousands of rows and pick out a few handful of information to make small changes based on some criteria. Using Openpyxl module, these tasks can be done very efficiently and easily.
    Use this command to install openpyxl module : 
     

    sudo pip3 install openpyxl 

     
    Input file : 
     

    Read excel file python openpyxl

    Code #1 : Program to print the particular cell value 
     

    Python3

    import openpyxl

    path = "C:\\Users\\Admin\\Desktop\\demo.xlsx"

    wb_obj = openpyxl.load_workbook(path)

    sheet_obj = wb_obj.active

    cell_obj = sheet_obj.cell(row = 1, column = 1)

    print(cell_obj.value)

    Output : 
     

    STUDENT 'S NAME

      
    Code #2 : Determine total number of rows 
     

    Python3

    import openpyxl

    path = "C:\\Users\\Admin\\Desktop\\demo.xlsx"

    wb_obj = openpyxl.load_workbook(path)

    sheet_obj = wb_obj.active

    print(sheet_obj.max_row)

    Output : 
     

    6

      
    Code #3 : Determine total number of columns 
     

    Python3

    import openpyxl

    path = "C:\\Users\\Admin\\Desktop\\demo.xlsx"

    wb_obj = openpyxl.load_workbook(path)

    sheet_obj = wb_obj.active

    print(sheet_obj.max_column)

    Output : 
     

    4

      
    Code #4 : Print all columns name 
     

    Python3

    import openpyxl

    path = "C:\\Users\\Admin\\Desktop\\demo.xlsx"

    wb_obj = openpyxl.load_workbook(path)

    sheet_obj = wb_obj.active

    max_col = sheet_obj.max_column

    for i in range(1, max_col + 1):

        cell_obj = sheet_obj.cell(row = 1, column = i)

        print(cell_obj.value)

    Output : 
     

    STUDENT 'S NAME
    COURSE
    BRANCH
    SEMESTER

      
    Code #5 : Print first column value 
     

    Python3

    import openpyxl

    path = "C:\\Users\\Admin\\Desktop\\demo.xlsx"

    wb_obj = openpyxl.load_workbook(path)

    sheet_obj = wb_obj.active

    m_row = sheet_obj.max_row

    for i in range(1, m_row + 1):

        cell_obj = sheet_obj.cell(row = i, column = 1)

        print(cell_obj.value)

    Output : 
     

    STUDENT 'S NAME
    ANKIT RAI
    RAHUL RAI
    PRIYA RAI
    AISHWARYA
    HARSHITA JAISWAL

      
    Code #6 : Print a particular row value 
     

    Python3

    import openpyxl

    path = "C:\\Users\\Admin\\Desktop\\demo.xlsx"

    wb_obj = openpyxl.load_workbook(path)

    sheet_obj = wb_obj.active

    max_col = sheet_obj.max_column

    for i in range(1, max_col + 1):

        cell_obj = sheet_obj.cell(row = 2, column = i)

        print(cell_obj.value, end = " ")

    Output : 
     

    ANKIT RAI B.TECH CSE 4

    Can openpyxl read XLS files?

    openpyxl is a Python library to read/write Excel 2010 xlsx/xlsm/xltx/xltm files.

    How read and write Excel file in openpyxl?

    Openpyxl is a Python library for reading and writing Excel (with extension xlsx/xlsm/xltx/xltm) files. The openpyxl module allows Python program to read and modify Excel files.

    How do I read an XLSX file in Python?

    Basically, here's the simplest form of using openpyxl for reading a xlsx file in Python:.
    import openpyxl from pathlib import Path xlsx_file = Path('SimData', 'play_data.xlsx') wb_obj = openpyxl.load_workbook(xlsx_file) # Read the active sheet: sheet = wb_obj.active. ... .
    import openpyxl from pathlib import Path..

    Can we read Excel file in Python?

    The read_excel function of the pandas library is used read the content of an Excel file into the python environment as a pandas DataFrame. The function can read the files from the OS by using proper path to the file. By default, the function will read Sheet1.