python - Openpyxl - how to populate data on specific sheet -
i'm using openpyxl first time. have read excel file, after manipulation, populate result on 3 different excel sheets -> sheet_t, sheet_d , sheet_u. created 3 sheets using openpyxl follows-
sheet_t = filename2.create_sheet(0) sheet_t.title = "target first" sheet_d = filename2.create_sheet(1) sheet_d.title = "distractor first" sheet_u = filename2.create_sheet(2) sheet_u.title = "unclassified"
i used xlwt there constraint of 256 columns. hence, used openpyxl. below code written using xlwt-
sheet_t.write(row_first, col_target, name_target) sheet_d.write(row_first, col_target, name_target) sheet_u.write(row_first, col_target, name_target)
how write same thing using openpyxl? documentation read how write on specific cell not sheet.
many help!
you need create sheet:
from openpyxl.workbook import workbook openpyxl.writer.excel import excelwriter wb = workbook() ws0 = wb.worksheets[0] ws0.title = 'my sheet 1' ws1 = wb.create_sheet() ws1.title = 'my sheet 2' ws2 = wb.create_sheet() ws2.title = 'my sheet 3'
now can write different sheets:
cell_ws0_a1 = ws0.cell('a1') cell_ws0_a1.value = 'wrote cell in 1st sheet.' cell_ws1_a1 = ws1.cell('a1') cell_ws1_a1.value = 'wrote cell in 2nd sheet.' cell_ws2_a1 = ws2.cell('a1') cell_ws2_a1.value = 'wrote cell in 3rd sheet.' writer = excelwriter(workbook=wb) writer.save('example.xlsx')
there 1 sheet in workbook default. wb.create_sheet()
creates second sheet.
Comments
Post a Comment