openpyxl sheet
2021. 12. 10. 13:18ㆍProgramming/Python
- 목차
반응형
openpyxl import
The first thing you need to do is import openpyxl as follow.
import openpyxl
from openpyxl.styles.borders import Border, Side
declare workbook
declare a new instance of Workbook
wb = openpyxl.Workbook()
create a new sheet
create the first sheet in the workbook.
wb.create_sheet('sheet 1', 0)
and get the reference to the sheet
sheet = wb.get_sheet_by_name('sheet 1')
adding cells
use Border for each cell
declare a variable abount border style
thin_border = Border(left=Side(style='thin'),
right=Side(style='thin'),
top=Side(style='thin'),
bottom=Side(style='thin'))
create cell
a cell is added with its own unique coordinate. So, we need to designate y, x coordinate as row, column.
for y in range(10):
for x in range(10):
sheet.cell(row=y + 1, column=x + 1).value = str(y) + ':' + str(x)
sheet.cell(row=y + 1, column=x + 1).border = thin_border
applying column width
widths = [10, 20, 10, 40, 80, 60, 50, 20, 20]
col_idx = ['A']
for i, width in enumerate(widths):
sheet.column_dimensions[''.join(col_idx)].width = width
if col_idx[-1] == 'Z':
col_idx += 'A',
else:
col_idx[-1] = chr(ord(col_idx[-1]) + 1)
반응형
'Programming > Python' 카테고리의 다른 글
Python access member as string (0) | 2021.12.10 |
---|---|
Python access member with string (0) | 2021.12.10 |
openpyxl sheet (0) | 2021.12.10 |
[Python] dictionary 응용 (0) | 2021.09.27 |
[Python] #2 변수와 입력 사용하기 (0) | 2021.09.27 |