xlwings is an open-source Python package that connects Excel with Python on Windows and Mac. It allows for interactive use from IPython Notebooks or any other Python environment but also allows to run Python code from Excel as replacement for VBA macros.
The talk will give an overview of the current Excel/Python landscape, show the characteristics of the xlwings package and include a live demo to show how easy the usage is. The demo will also include examples of tasks that would otherwise be difficult to do with Excel only as well as more real world tools built with Python where Excel merely acts as a user interface/frontend.
xlwings is a BSD-licensed Python library that makes it easy to call Python from Excel and vice versa:
Interact with Excel from Python using a syntax that is close to VBA yet Pythonic.
Replace your VBA macros with Python code and still pass around your workbooks as easy as before.
xlwings fully supports NumPy arrays and Pandas DataFrames.
xlwings is an open-source Python package that connects Excel with Python on Windows and Mac. It allows for interactive use from IPython Notebooks or any other Python environment but also allows to run Python code from Excel as replacement for VBA macros.
The talk will give an overview of the current Excel/Python landscape, show the characteristics of the xlwings package and include a live demo to show how easy the usage is. The demo will also include examples of tasks that would otherwise be difficult to do with Excel only as well as more real world tools built with Python where Excel merely acts as a user interface/frontend.
xlwings is a BSD-licensed Python library that makes it easy to call Python from Excel and vice versa:
Interact with Excel from Python using a syntax that is close to VBA yet Pythonic.
Replace your VBA macros with Python code and still pass around your workbooks as easy as before.
xlwings fully supports NumPy arrays and Pandas DataFrames.
2. Contents!
Python Excel module
Install module(via pip)
Excel with Python
Example code #1(xlrd)
Example code #2(xlwt)
Example code #3(openpyxl)
Reference
3. Python Excel module
Excel VBA처럼 Python에서도 Excel file을 읽고 쓸 수 있는 module들이 있음
여기서는 다음 두 가지만 설명
1) Xlrd(excel read) & xlwt(excel write): 활용하기 제일 쉬움
• 어차피 복잡한 수식 등은 python에서 직접 코딩하거나 엑셀에서 직접 처리하는게 편함
• 다만 xlwt에서는 xlsx 파일 지원하지 않음(xls만 지원), 따라서 최대 처리 행 수 65536으로 제한
2) OpenPyXl
• Documentation도 잘되어 있으며, python에서 가장 많이 쓰이는 module
3) Xlswriter(write only)
• Documentation이 잘 되어 있어서 활용하기 편함
4) PyExceleratre
https://libsora.so/posts/python-excel-library/
4. Install Package(via pip)
앞서 설명한 라이브러리를 활용하기 위해서는 module들을 하나하나 직접 설치해야 함
제어판 ==> 시스템 및 보안 ==> 시스템 ==> 고급 시스템 설정 ==>
고급 탭 ==> 환경 변수 ==> 시스템 변수, Path 변수에 Python이 설치된 폴더 경로를 입력
(기존 내용을 삭제하지 않고 “;”으로 구분하여 입력)
완료하면, 윈도우+r ==> cmd에서 바로 python으로 실행 가능
ex: pip install pandas
ex: pip install numpy
ex: pip install xlrd
ex: pip install xlwt
if it didn’t work
pip install –-upgrade pip
5. Example Code #1(xlrd. Excel Read)
import xlrd
workbook=xlrd.open_workbook("xlrd_test.xlsx")#read excel file
worksheet1 = workbook.sheet_by_index(0) #Find worksheet 0(첫번째 시트)
n_rows=worksheet1.nrows #sheet 내에 전체 row 수를 return
row_list=[] #각 row를 넣을 list 생성
for row_num in range(n_rows): #모든 row에 대해서 list에 값을 집어넣음
row_list.append(worksheet1.row_values(row_num))
#row_list에 모든 row가 element로 모두 들어감
6. Example Code #2(Xlwt, Write Excel)
import xlwt
workbook=xlwt.Workbook(encoding="utf-8") #utf-8 방식의 Workbook 생성
worksheet=workbook.add_sheet("kkk") # 시트이름이 “kkk”인 시트 추가
for row_index in range(0, 100):
for col_index in range(0, 100):
worksheet.write(row_index, col_index,0) #row_index, col_index에 0을 입력
#100x100 matrix를 엑셀파일 해당 시트에 입력완료
workbook.save("xlwt_test.xls") # 엑셀 파일 저장
# 앞서 말했듯이 xlwt에서는 xlsx를 지원하지 않음.
7. Example Code #3(Openpyxl, Write Excel)
From openpyxl import Workbook
From openpyxl.cell import get_column_letter#해당 cell의 column letter return(예: A6 ==> A)
Wb=Workbook()#wookbook 인스턴스 생성
Dest_filename=“empty_book.xlsx” #file name
Ws1=wb.active#활성화되어 있는 시트를 활용
Ws2=wb.create_sheet(title=“Pi”) #이름이 “PI”인 시트 생성
Ws1.title=“range names” #시트의 이름을 변경
For row in range(1, 40):#cell index 시작이 1
ws1.append(range(600)) #0부터 599까지의 list를 각 row에 입력
#39x600 matrix가 생성
For col_index in range(1, 20):
ws1.cell(row=40, column=col_index, value="=sum("+get_column_letter(col_index)+"1:"+get_column_letter(col_index)+"39)")
#function을 string으로 집어넣음
ws2[“F5”]=3.14#F5에 3.14를 집어넣음
Wb.save(“empty_book.xlsx”)
8. Example Code #4(Openpyxl, read excel)
From openpyxl import load_workbook
Wb=load_workbook(filename=“empty_book.xlsx”) # “empty_book.xlsx”를 읽음
Sheet1=wb[“range names”] #시트 설정 “range names”라는 시트를 sheet1에 저장
Print(sheet1[“D18”].value) #sheet1의 D18에 있는 값을 출력