카테고리 없음
openpyxl sheet
반응형
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)
반응형
댓글