Как открыть файл excel в jupiter

I have tried to read an xlsx file which is stored in my system using jupyter notebook.I had run the jupyter notebook from the present directory where my file is present.But it is showing an error as «module not found error».

This is the image of the code i have written and also the error i got

This is the complete traceback

asked Oct 6, 2017 at 5:44

Akhil Reddy's user avatar

Akhil ReddyAkhil Reddy

3611 gold badge6 silver badges26 bronze badges

6

The below code should able to access /read your excel file

import pandas as pd
path = ('...\filename.xlsx')
xl = pd.ExcelFile(path)
print(xl.sheet_names)

The above command shows the sheets in a xlsx file

df1 = xl.parse('Sheet1')

Above command parses the sheet required

Jolta's user avatar

Jolta

2,6201 gold badge31 silver badges42 bronze badges

answered Jun 15, 2018 at 23:28

Hemanth Kocherlakota's user avatar

Same outout, but this one worked for me.

import pandas as pd

df = pd.read_excel (r'C:Users(NAME)Desktop(FILENAME).xlsx')
print (df)

answered May 25, 2020 at 14:32

Marcell Kovacs's user avatar

import pandas as pd
df = pd.read_excel('file_name.xlsx', 'Sheet1')
df

*you must import your .xlsx file into the Jupyter notebook file…
*you may also import it into a Github repository and get the raw file then just copy and paste it into where it says ‘file_name.xlsx’

[raw file URL example][1]

answered Dec 27, 2020 at 3:05

Jonathan Um's user avatar

import pandas as pd
wb = pd.ExcelFile('D:\<Excel File Name>.xlsx')
ds = wb.parse('<Sheet  Name>')
print(ds)

answered Mar 27, 2020 at 13:43

ARVIND CHAUHAN's user avatar

Я попытался прочитать файл xlsx, который хранится в моей системе, используя блокнот jupyter. Я запустил блокнот jupyter из текущего каталога, где находится мой файл. Но он показывает ошибку как «ошибка модуля не найдена».

This is the image of the code i have written and also the error i got

This is the complete traceback

3 ответа

Лучший ответ

Приведенный ниже код должен иметь возможность доступа / чтения вашего файла Excel

import xlsxwriter

import pandas as pd

from pandas import DataFrame

path = ('...\filename.xlsx')

xl = pd.ExcelFile(path)

print(xl.sheet_names)

Приведенная выше команда показывает листы в файле xlsx

df1 = xl.parse('Sheet1')

Над командой разбирает требуемый лист


1

David
16 Июн 2018 в 06:43

import pandas as pd
wb = pd.ExcelFile('D:\<Excel File Name>.xlsx')
ds = wb.parse('<Sheet  Name>')
print(ds)


0

ARVIND CHAUHAN
27 Мар 2020 в 13:43

Тот же вывод, но этот работал для меня.

import pandas as pd

df = pd.read_excel (r'C:Users(NAME)Desktop(FILENAME).xlsx')
print (df)


0

Marcell Kovacs
25 Май 2020 в 14:32

It used to be an “either/or” choice between Excel and Python Jupyter Notebooks. With the introduction of the PyXLL-Jupyter package now you can use both together, side by side.

In this post I’ll show you how to set up Jupyter Notebooks running inside Excel. Share data between the two and even call Python functions written in your Jupyter notebook from your Excel workbook!

Getting Started

First off, to run Python code in Excel you need the PyXLL add-in. The PyXLL add-in is what lets us integrate Python into Excel and use Python instead of VBA. To install the PyXLL Excel add-in pip install pyxll and then use the PyXLL command line tool to install the Excel add-in:

> pip install pyxll
> pyxll install

If you’re new to PyXLL then take a look at the online documentation for first time users to help get you started.

Once you have the PyXLL Excel add-in installed the next step is to install the pyxll-jupyter package. This package provides the glue between PyXLL and Jupyter so that we can use our Jupyter notebooks inside of Excel.

The pyxll-jupyter package is installed using pip:

> pip install pyxll-jupyter

Once both the PyXLL Excel add-in and the PyXLL-Jupyter package are installed start Excel and you will see a new “Jupyter” button in the PyXLL tab.

Button to open Jupyter in Excel

Clicking this button opens the Jupyter notebook in a side panel in your Excel workbook. This panel is part of the Excel interface and can be un-docked or docked in a different location by dragging it.

In the Jupyter panel you can select an existing notebook or create a new one. To create a new notebook select the “New” button followed by “Python 3”.

Starting with version 0.2.0 of the pyxll-jupyter package you can open the Jupyter notebook in a browser as well as in an Excel task pane. If you are working across multiple screens this can make things easier! To upgrade to the latest version run pip install --upgrade pyxll-jupyter.

How is this useful?

Now you have a complete Jupyter notebook running inside of Excel! But what is this good for? How is this better than running a notebook outside of Excel?

Well, now you can use Excel for working with your data and use Python to work on the same data. Use Excel as an interactive playground for organizing and visualizing your data, seamlessly switching to Python for more sophisticated tools.

Use a Jupyter notebook as a scratch-pad for trying out Python code. Write Excel functions entirely in Python in a Jupyter notebook and test them out in real-time. Once you’ve developed a useful re-usable function add it to your PyXLL Python project. That way you can use the same function every time you use Excel.

In the rest of this post I’ll show you how to:

  • Share data between Excel and Python using your Jupyter notebook
  • Write Excel worksheet functions (UDFs) in your notebook
  • Script Excel with Python instead of VBA

Getting data from Excel into Python

Because PyXLL runs Python in the same process as Excel, accessing Excel data in Python and calling between Python and Excel is fast.

To make things as easy as possible, the pyxll-jupyter package comes with some IPython “magic” functions for you to use in your Jupyter notebooks.

%xl_get

Use the magic function “%xl_get” to get the current Excel selection in Python. Have a table of data in Excel? Select the top left corner (or the whole range) and type “%xl_get” in your Jupyter notebook and voila! the Excel table is now a pandas DataFrame.

The %xl_get magic function requires the “pywin32” package. You can install this by running:

pip install “pywin32==228”

If you get a “DLL Load Failed” error then it may be caused by a bug in version 300 of the pywin32 package. Use the above command to install the previous version (228). This bug has been fixed in pywin32, but not yet released.

The %xl_get magic function as several options:

  • -c or --cell. Pass the address of the cell(s) to get the value of, eg %xl_get --cell A1:D5.
  • -t or --type. Specify a data type to use when getting the value, eg %xl_get --type numpy_array.
  • -x or --no-auto-resize. Only get the data for the selected or given range. Don’t expand to include the surrounding range of data.

PyXLL has other ways of interacting with Excel to read data into Python. The “%xl_get” magic function is just a shortcut to make things easier! As the Jupyter notebook is running in Excel, all other methods (eg using the XLCell class, Excel’s COM API or even xlwings) are still available.

TIP: You can assign a variable to the result of a magic function! For example, try “df = %xl_get”.

Moving data in Python back to Excel

Transfering data the other way around, from Python to Excel, works just as well. Whether you’ve used Python to load a dataset and want to transfer it to your Excel workbook, or if you’ve manipulated a data set from Excel and want the results back in Excel, copying data to Excel from Python is easy.

%xl_set

The magic function “%xl_set” takes a Python object and writes it to Excel. Have a dataframe “df” that you want in Excel? No problem, just use “%xl_set df” and it will be written to the current selection in Excel.

The %xl_set magic function requires the “pywin32” package. You can install this by running:

pip install “pywin32==228”

If you get a “DLL Load Failed” error then it may be caused by a bug in version 300 of the pywin32 package. Use the above command to install the previous version (228). This bug has been fixed in pywin32, but not yet released.

Like %xl_get, %xl_set has a range of options to control its behaviour. You can even use PyXLL’s cell formatting feature to automatically apply formatting at the same time as writing results to Excel.

  • -c or --cell. Address of cell(s) to write the value to, eg %xl_set VALUE --cell A1.
  • -t or --type. Datatype specifier to use when writing the value to Excel, eg %xl_set VALUE --type dataframe<index=False>.
  • -f or --formatter. PyXLL cell formatter object, eg %xl_set VALUE --formatter DataFrameFormatter(). See cell formatting.
  • -x or --no-auto-resize. Don’t auto-resize the range to fit the data. Only write values to the current selection or specified range.

As with %xl_get, %xl_set is meerly a shortcut and all the other ways of writing back to Excel that you might have used with PyXLL will still work in a Jupyter notebook.

Use Python plots (matplotlib/plotly etc) in Excel

One of the great things about working with data is the powerful plotting packages available. Being able to plot a pandas DataFrame with a simple “df.plot()” is awesome!

PyXLL has integration with all of the main plotting libraries so you can make the most of these in Excel too. This includes matplotlib (used by pandas), plotly, bokeh and altair.

%xl_plot

Use “%xl_plot” to draw any Python chart in Excel. Pass it any figure object from one of the supported plotting libraries, or use the last pyplot figure. Using pandas plot works great, eg. %xl_plot df.plot(kind='scatter').

The %xl_plot magic function has some options to control how it works:

  • -n or --name. Name of the picture object in Excel. If using a name of a picture that already exists that picture will be replaced.
  • -c or --cell. Cell address to use as the location for the new picture. If the picture already exists this has no effect.
  • -w or --width. Width of the picture in Excel in points. This has no effect if updating an existing picture.
  • -h or --height. Height of the picture in Excel in points. This has no effect if updating an existing picture.

%xl_plot is a shortcut for the pyxll.plot function.

Call Python functions from Excel

Rather than constantly moving data between Excel and Jupyter and then running some Python code, you can call Python function directly from the Excel workbook!

One of the main use-cases for PyXLL is writing custom Excel worksheet functions (or “UDFs”) in Python. This is used for building models in Excel built from Python functions, which can of course themselves use other Python toolkits like pandas and scipy.

You can write Excel worksheet functions in your Jupyter notebook too. This is a really great way of trying out ideas without leaving Excel to go to a Python IDE.

Try it out for yourself. Write a simple function and then add the “pyxll.xl_func” decorator to your function:

from pyxll import xl_func

@xl_func
def test_func(a, b, c):
    # This function can be called from Excel!
    return (a * b) + c

After you’ve entered the code and run the cell in Jupyter that Python function will immediately be available to call from the Excel workbook.

It’s not just for simple functions. You can pass whole ranges of data to your function as pandas DataFrames and return any Python type, including numpy arrays and DataFrames! You can tell PyXLL what types to expect by giving the @xl_func decorator a signature string.

For example, try out the following:

from pyxll import xl_func

# The "signature" tells PyXLL how to convert the arguments
# and returned value.
@xl_func("dataframe df: dataframe<index=True>", auto_resize=True)
def df_describe(df):
    # 'df' is a pandas DataFrame built from the range passed
    # to this function.
    desc = df.describe()

    # 'desc' is a new DataFrame, which PyXLL will convert to
    # a range of values when returning to Excel.
    return desc

Now you can write complex Python functions to do data transformation and analysis, but orchestrate how those functions are called or sequenced in Excel. Changing the inputs results in the functions be called and the calculated outputs update in real-time, just as you would expect!

Script Excel with Python instead of VBA

Did you know that everything you can do in VBA can also be done in Python? The Excel Object Model is what you use when writing VBA, but the same API is available in Python as well.

See Python as a VBA Replacement from the PyXLL documentation for details of how this is possible.

In a Jupyter notebook running in Excel the entire Excel Object Model is available and so you can script Excel in exactly the same way you might do in the Excel VBA Editor.

Because PyXLL runs Python inside the Excel process there is no performance penalty for calling into Excel from Python. It is also possible to call into Excel from an external Python process, but this is generally much slower. Having a Jupyter notebook running in Excel makes everything more convenient too!

Use PyXLL’s xl_app function to get the “Excel.Application” object, which is equivalent to the Application object in VBA. Try something like getting the current selection and changing the cell interior color. A great way of figuring out how to do something with the Excel Object Model is to record a VBA Macro and then translate that macro into Python! The PyXLL docs page Python as a VBA Replacement has some tips on how to do that.

Summary

Python makes a poweful alternative to VBA. With PyXLL you can write fully featured Excel add-ins entirely in Python. Excel is an amazing tool for interactive computation. Adding Python and Jupyter takes Excel to a whole new level.

Code written in Jupyter notebooks can easily be refactored into standalone Python packages to create Excel tool-kits to power intutitive workbooks and dashboards. Any Excel user will be able to take advantage of Python tools written using PyXLL without needing any knowledge of Python.

If you want to find out more then download and try PyXLL for yourself or contact us. We’ll be happy to answer any questions you might have and find out how we can help!

Back to top

Edit this page

Toggle table of contents sidebar

When you work with Jupyter notebooks, you may use Excel as an interactive data viewer or scratchpad from where you can load DataFrames. The two convenience functions view and load make this really easy.

Note

The view and load functions should exclusively be used for interactive work. If you write scripts, use the xlwings API as introduced under Quickstart and Syntax Overview.

The view function#

The view function accepts pretty much any object of interest, whether that’s a number, a string, a nested list or a NumPy array or a pandas DataFrame. By default, it writes the data into an Excel table in a new workbook. If you wanted to reuse the same workbook, provide a sheet object, e.g. view(df, sheet=xw.sheets.active), for further options see view.

_images/xw_view.png

Changed in version 0.22.0: Earlier versions were not formatting the output as Excel table

The load function#

To load in a range in an Excel sheet as pandas DataFrame, use the load function. If you only select one cell, it will auto-expand to cover the whole range. If, however, you select a specific range that is bigger than one cell, it will load in only the selected cells. If the data in Excel does not have an index or header, set them to False like this: xw.load(index=False), see also load.

_images/xw_load.png

New in version 0.22.0.

enter image description here

I could not open my excel file in jupter lab

Also when i tried to open file in jupyter notebook using dataframe its showing file not found.

Chris Albert's user avatar

Chris Albert

2,4618 gold badges29 silver badges31 bronze badges

asked May 11, 2020 at 2:26

online Deb's user avatar

1

Open it using excel and save it as a csv file. Then you can open the file. The excel format is not supported to jupyter lab that’s why it is not opening. You have to convert it to UTF-8 comma delimited format to be able to read in jupyter lab

answered May 11, 2020 at 4:46

Gayal Kuruppu's user avatar

You might consider using a 3rd party plugin such as this one to view .xlsx files:

https://github.com/quigleyj97/jupyterlab-spreadsheet

Installing is simple:

jupyter labextension install jupyterlab-spreadsheet

Then refresh/reload the JupyterLab browser window

answered May 3, 2021 at 13:18

Alex L's user avatar

Alex LAlex L

4,1361 gold badge8 silver badges24 bronze badges

3

Понравилась статья? Поделить с друзьями:

А вот еще интересные статьи:

  • Как открыть файл excel большого размера
  • Как открыть файл csv в excel без иероглифов
  • Как открыть файл excel без пароля
  • Как открыть файл asd если word не открывает
  • Как открыть файл excel application

  • 0 0 голоса
    Рейтинг статьи
    Подписаться
    Уведомить о
    guest

    0 комментариев
    Старые
    Новые Популярные
    Межтекстовые Отзывы
    Посмотреть все комментарии