R
R
Roman Romanov2019-07-31 18:29:43
Python
Roman Romanov, 2019-07-31 18:29:43

How to use data from excel spreadsheets in python script?

Hello, there are a number of Excel tables in which numeric data is in two columns
. Is it possible to use this data in some way? You need to refer to them somehow, use them in a script

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
sazhyk, 2019-08-01
@avelecter

Alexander has already advised openpyxl. I will answer in a slightly different way.
To get started, it would be nice to see an example of the file you want to parse.
No need to import to CSV. Openpyxl can do everything by itself.

from openpyxl import load_workbook

wb = load_workbook("/path/to/file.xls")           # Открываем файл
sheet = wb.get_sheet_by_name('New Sheet')         # Выбираем лист
# Выбираем диапазон заполненных ячеек
max_row = sheet.max_row                           # Последняя заполненная строка
max_col = sheet.max_column                        # Последняя заполненная колонка
for r in range(1, max_row + 1):
    for c in range(1, max_column + 1):
        val = sheet.cell(row=r, column=c).value   # Получаем в цикле значение в конкретной ячейке
        # do something

Then do whatever you want with your data. The example may not be perfect, but I think the point is clear. Liba is pretty easy to learn. If you need to work more seriously with Excel data - take the advice of freelion93 and use pandas. It seemed to me more difficult to master, but the functionality there is much wider. But this is purely my personal opinion.

F
freelion93, 2019-07-31
@freelion93

Save the Excel file in CSV format, and it is convenient to process it in the pandas dataframe

#устанавливаем pandas
!pip install pandas
#импортируем библиотеку pandas
import pandas as pd 
# Читаем из 'excel.csv' 
data = pd.read_csv("excel.csv") 
# Выводим первые строчки
data.head()

A short pandas tutorial

A
Alexander, 2019-07-31
@NeiroNx

Save to CSV - the original solution, I use this one:
https://openpyxl.readthedocs.io/en/stable/
Reads, writes

V
Vladimir Kuts, 2019-07-31
@fox_12

xlrd

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question