-1

I would like to open .xlsx file using python for further manual process. I tried

wb = load_workbook(filename = 'empty_book.xlsx')

by importing openpyxl module. But it does not open the file rather it just loads the file. Is there any other way to open excel file in python?

Thanks in advance

Ashish Ranjan
  • 5,523
  • 2
  • 18
  • 39

2 Answers2

1

You could use pandas (also you need to install module xlrd)

import pandas as pd


excel_data = pd.read_excel('empty_book.xlsx')
Damir Shakenov
  • 331
  • 4
  • 17
  • I am getting following error even if pandas and xlrd is installed >>> import pandas as pd Traceback (most recent call last): File "", line 1, in import pandas as pd File "C:\Python27\lib\site-packages\pandas\__init__.py", line 35, in "the C extensions first.".format(module)) ImportError: C extension: NaT not built. If you want to import pandas from the source directory, you may need to run 'python setup.py build_ext --inplace --force' to build the C extensions first. – Radhicka Pardessi Oct 27 '17 at 10:38
  • @RadhickaPardessi check this link https://stackoverflow.com/questions/30761152/how-to-solve-import-error-for-pandas – Damir Shakenov Oct 27 '17 at 10:52
  • Is there some advantage in using pandas instead of openpyxl? – Fábio Roberto Teodoro Jun 02 '21 at 20:33
1

Import openpyxl

>>> import openpyxl

Load the workbook that you are trying to read

>>> wb = openpyxl.load_workbook("Empty.xlsx")

Give the name of the sheet

>>> ws = wb['sheet_name']

For looping through values in the excel

for row in ws.rows:
        for cell in row:
              print cell.value

To edit the values

for row in ws.rows:
        for cell in row:
              cell.value = new_value
Siva
  • 509
  • 6
  • 22