012. excel with python
# @
# 자치단체 행정구역 및 인구현황
# www.index.go.kr/potal/main/EachDtlPageDetail.do?idx_cd=1041
# Save as same name and 'Excel 통합문서 format'
# @
# Excel file is called book
# and there can be lots of sheets in excel file
# Spreadsheet is composed of rows and columns
# @
# You first install openpyxl module
# which will be used to read excel file in python
pip3 install openpyxl
# @
import openpyxl
# First, you need to open excel file(book)
book = openpyxl.load_workbook("stats_104102.xlsx")
# For a referernce, you can use various methods which start with get
# Check them out by typing book.get
# For example, you can bring name of sheet by book.get_sheet_names()
# You can bring specific sheet by informing its name
# with book.get_sheet_by_name('stats_104102')
# And you can bring sheets by another way as following
sheet = book.worksheets[0]
for row in sheet.rows:
for data in row:
# I delimit each end part of value by white space
print(data.value, end=" ")
print("", end="\n")
# @
# I will try to write data onto excel by python
# First, I need to create Workbook instance
workbook = openpyxl.Workbook()
# I bring active sheets in workbook
sheet = workbook.active
# I write data onto excel file
sheet["A1"] = "test file"
sheet["B1"] = "hello"
sheet.merge_cells("A1:C1")
sheet["A1"].font = openpyxl.styles.Font(size=20, color="FF0000")
# Finally, I save written excel instance as excel file
workbook.save("newFile.xlsx")