Python에서 Excel 파일을 어떻게 열 수 있습니까?


87

Python에서 읽기 위해 Excel 파일 인 파일을 어떻게 열 수 있습니까?

예를 들어 sometextfile.txt읽기 명령을 사용하여 텍스트 파일을 열었습니다 . Excel 파일에 대해 어떻게 수행합니까?


2
어떤 버전의 Excel? Ecel 2007 또는 2010에서 만든 Excel 파일을 열 수 있도록 제한 할 수있는 경우 파일의 대부분 또는 전체를 XML로 구문 분석 할 수 있어야합니다.
— Adam Crossland

답변:


97

편집 :
최신 버전의 pandas에서는 시트 이름을 매개 변수로 전달할 수 있습니다.

file_name =  # path to file + file name
sheet =  # sheet name or sheet number or list of sheet numbers and names

import pandas as pd
df = pd.read_excel(io=file_name, sheet_name=sheet)
print(df.head(5))  # print first 5 rows of the dataframe

전달 방법에 대한 예제는 문서를 확인하십시오 sheet_name.
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_excel.html

이전 버전 : 패키지 도
사용할 수 있습니다 ....pandas

여러 시트가있는 Excel 파일로 작업 할 때 다음을 사용할 수 있습니다.

import pandas as pd
xl = pd.ExcelFile(path + filename)
xl.sheet_names

>>> [u'Sheet1', u'Sheet2', u'Sheet3']

df = xl.parse("Sheet1")
df.head()

df.head() Excel 파일의 처음 5 개 행을 인쇄합니다.

단일 시트가있는 Excel 파일로 작업하는 경우 다음을 사용하면됩니다.

import pandas as pd
df = pd.read_excel(path + filename)
print df.head()

2
이 솔루션은 내 업 보트를 얻습니다. openpyxl을 사용하면 "InvalidFileException : openpyxl이 이전 .xls 파일 형식을 지원하지 않습니다. xlrd를 사용하여이 파일을 읽거나 최신 .xlsx 파일 형식으로 변환하십시오"라는 문제가 발생합니다. 반면에 pandas는 .xls 및 .xlsx 파일을 모두 처리합니다. 또한 전체 테이블을 읽는 데 한 줄의 코드 만 필요합니다.
— nathanielng

3
xlrdExcel 파일 읽기 및 xlwtExcel 파일 쓰기를 위한 선택적 종속성을 설치해야 합니다.
— Flimm

32

xlrd 라이브러리를 사용해보십시오 .

[편집] -귀하의 의견에서 볼 수있는 것에서 아래 스 니펫과 같은 것이 트릭을 수행 할 수 있습니다. 여기서는 'john'이라는 단어에 대해 하나의 열만 검색한다고 가정하고 있지만 더 추가하거나 더 일반적인 함수로 만들 수 있습니다.

from xlrd import open_workbook

book = open_workbook('simple.xls',on_demand=True)
for name in book.sheet_names():
    if name.endswith('2'):
        sheet = book.sheet_by_name(name)

        # Attempt to find a matching row (search the first column for 'john')
        rowIndex = -1
        for cell in sheet.col(0): # 
            if 'john' in cell.value:
                break

        # If we found the row, print it
        if row != -1:
            cells = sheet.row(row)
            for cell in cells:
                print cell.value

        book.unload_sheet(name) 

나는 이것이 내가 원하는 일이라고 생각한다 : from xlrd import open_workbook book = open_workbook ( 'simple.xls', on_demand = True) for name in book.sheet_names () : if name.endswith ( '2') : sheet = book.sheet_by_name (name) print sheet.cell_value (0,0) book.unload_sheet (name) large_files.py하지만 endwith를 사용하고 싶지는 않지만 특정 이름이 포함 된 줄을 찾아서 인쇄하고 싶습니다. 나는 bob이 아닌 john의 데이터를 포함하는 거대한 엑셀 시트의 라인을 인쇄하고 싶습니다. 도움?
— novak 2010

나는 이것을 별도의 질문으로 게시하고 코드 블록에 코드를 넣는 것이 좋습니다.
— Jon Cage

이것은 일련의 관련 질문 중 두 번째 질문입니다. 세 번째 질문에서 실제 엑셀 파일은 1.5GB로 추정되며 컴퓨터의 메모리는 "충분하지 않음"으로 설명되어 있습니다 ... 참조 stackoverflow.com/questions/3241039/…
— John Machin

16

이것은 일반 텍스트 파일을 여는 것만 큼 간단하지 않으며이를 수행하기 위해 내장 된 것이 없기 때문에 일종의 외부 모듈이 필요합니다. 다음은 몇 가지 옵션입니다.

http://www.python-excel.org/

가능한 경우 Excel 스프레드 시트를 CSV 파일로 내 보낸 다음 내장 된 python csv 모듈을 사용하여 읽을 수 있습니다.

http://docs.python.org/library/csv.html


좋아, 나는 CSV에 대해 정말로 이해하지 못한다. 어떻게 파이썬이 내 엑셀 파일을 csv 모듈로 열 수 있습니까? txt 파일에 대해 원하는 것을 수행하는 프로그램이 있고이 Excel 파일에 대해 동일한 작업을 수행하기를 원합니다. 가장 좋은 방법은 무엇입니까? 이에 대해 자세히 설명해 주시겠습니까?
— novak 2010

xlrd와 같은 타사 Python 모듈을 사용하거나 일반 Excel 파일 대신 Excel 파일을 CSV 파일로 저장할 수 있습니다. 당신이 놓친 요점은 엑셀 파일이 일반 텍스트 파일과 유사하지 않다는 것입니다. 메모장에서 Excel 문서를 열면 무슨 뜻인지 알 수 있습니다. CSV (쉼표로 구분 된 값)와 같은 일반 텍스트 형식으로 파일을 저장해야합니다. CSV (쉼표로 구분 된 값)는 Python으로 읽기가 더 쉽습니다. 또는 Excel 파일을 구문 분석 할 수있는 타사 모듈을 설치하여 사용해야합니다.
— Donald Miner

내가 가진 문제는 파일이 정말 크다는 것입니다. 파일을 완전히 열 수없는 경우 파일을 CSV 형식으로 저장하려면 어떻게해야합니까?
— novak 2010

@novak : 귀하의 문제는 ... 당신의 파일이 1.5GB이며, 컴퓨터의 메모리가 "충분하지"이다
— 존 머신

6

있다 openpxyl의 패키지 :

>>> from openpyxl import load_workbook
>>> wb2 = load_workbook('test.xlsx')
>>> print wb2.get_sheet_names()
['Sheet2', 'New Title', 'Sheet1']

>>> worksheet1 = wb2['Sheet1'] # one way to load a worksheet
>>> worksheet2 = wb2.get_sheet_by_name('Sheet2') # another way to load a worksheet
>>> print(worksheet1['D18'].value)
3
>>> for row in worksheet1.iter_rows():
>>>     print row[0].value()


1

도움이 될 수 있습니다.

이렇게하면 2D 목록 (목록 항목 목록)을 가져 와서 Excel 스프레드 시트로 푸시하는 노드가 생성됩니다. IN []이 있거나 예외가 발생하는지 확인하십시오.

이것은 기본 사전 패키지 노드가 계속 중단됨에 따라 Excel 2013 용 Revit excel dynamo 노드를 다시 작성한 것입니다. 비슷한 읽기 노드도 있습니다. Python의 엑셀 구문은 까다 롭습니다.

thnx @CodingNinja-업데이트 됨 :)

###Export Excel - intended to replace malfunctioning excel node

import clr

clr.AddReferenceByName('Microsoft.Office.Interop.Excel, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c')
##AddReferenceGUID("{00020813-0000-0000-C000-000000000046}") ''Excel                            C:\Program Files\Microsoft Office\Office15\EXCEL.EXE 
##Need to Verify interop for version 2015 is 15 and node attachemnt for it.
from Microsoft.Office.Interop import  * ##Excel
################################Initialize FP and Sheet ID
##Same functionality as the excel node
strFileName = IN[0]             ##Filename
sheetName = IN[1]               ##Sheet
RowOffset= IN[2]                ##RowOffset
ColOffset= IN[3]                ##COL OFfset
Data=IN[4]                      ##Data
Overwrite=IN[5]                 ##Check for auto-overwtite
XLVisible = False   #IN[6]      ##XL Visible for operation or not?

RowOffset=0
if IN[2]>0:
    RowOffset=IN[2]             ##RowOffset

ColOffset=0
if IN[3]>0:
    ColOffset=IN[3]             ##COL OFfset

if IN[6]<>False:
    XLVisible = True #IN[6]     ##XL Visible for operation or not?

################################Initialize FP and Sheet ID
xlCellTypeLastCell = 11                 #####define special sells value constant
################################
xls = Excel.ApplicationClass()          ####Connect with application
xls.Visible = XLVisible                 ##VISIBLE YES/NO
xls.DisplayAlerts = False               ### ALerts

import os.path

if os.path.isfile(strFileName):
    wb = xls.Workbooks.Open(strFileName, False)     ####Open the file 
else:
    wb = xls.Workbooks.add#         ####Open the file 
    wb.SaveAs(strFileName)
wb.application.visible = XLVisible      ####Show Excel
try:
    ws = wb.Worksheets(sheetName)       ####Get the sheet in the WB base

except:
    ws = wb.sheets.add()                ####If it doesn't exist- add it. use () for object method
    ws.Name = sheetName



#################################
#lastRow for iterating rows
lastRow=ws.UsedRange.SpecialCells(xlCellTypeLastCell).Row
#lastCol for iterating columns
lastCol=ws.UsedRange.SpecialCells(xlCellTypeLastCell).Column
#######################################################################
out=[]                                  ###MESSAGE GATHERING

c=0
r=0
val=""
if Overwrite == False :                 ####Look ahead for non-empty cells to throw error
    for r, row in enumerate(Data):   ####BASE 0## EACH ROW OF DATA ENUMERATED in the 2D array #range( RowOffset, lastRow + RowOffset):
        for c, col in enumerate (row): ####BASE 0## Each colmn in each row is a cell with data ### in range(ColOffset, lastCol + ColOffset):
            if col.Value2 >"" :
                OUT= "ERROR- Cannot overwrite"
                raise ValueError("ERROR- Cannot overwrite")
##out.append(Data[0]) ##append mesage for error
############################################################################

for r, row in enumerate(Data):   ####BASE 0## EACH ROW OF DATA ENUMERATED in the 2D array #range( RowOffset, lastRow + RowOffset):
    for c, col in enumerate (row): ####BASE 0## Each colmn in each row is a cell with data ### in range(ColOffset, lastCol + ColOffset):
        ws.Cells[r+1+RowOffset,c+1+ColOffset].Value2 = col.__str__()

##run macro disbled for debugging excel macro
##xls.Application.Run("Align_data_and_Highlight_Issues")

@CodingNinja 충분합니까? :)
— Apsis0215

네 훨씬 나아

0

이 코드는 Python 3.5.2에서 저에게 효과적이었습니다. 열리고 저장하고 뛰어납니다. 현재 파일에 데이터를 저장하는 방법에 대해 작업 중이지만 코드는 다음과 같습니다.

import csv
excel = csv.writer(open("file1.csv", "wb"))

 


-1
import pandas as pd 
import os 
files = os.listdir('path/to/files/directory/')
desiredFile = files[i]
filePath = 'path/to/files/directory/%s'
Ofile = filePath % desiredFile
xls_import = pd.read_csv(Ofile)

이제 Pandas DataFrames의 강력한 기능을 사용할 수 있습니다!


1
문제는 쉼표로 구분 된 텍스트 파일이 아니라 Excel 파일을 읽는 것입니다. Pandas에는 ( pandas.read_excel)에 대한 기능이있는 것 같습니다 .
— Bart
당사 사이트를 사용함과 동시에 당사의 쿠키 정책과 개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.