import openpyxl
list = ["X", "Alpha", "Beta", "Gamma", "B/A", "C/A", "C/B"]
initSet = ['0.1', '0.2', '0.3', '0.4','0.5','0.6','0.7','0.8','0.9']
A = [0.3, 0.5, 0.7, 0.4, 0.2, 0.6, 0.4, 0.8, 0.2]
B = [0.1, 0.3, 0.5, 0.7, 0.9, 0.5, 0.3, 0.2, 0.1]
C = [0.1, 0.4, 0.2, 0.1, 0.5, 0.3, 0.2, 0.1, 0.5]
def Part1():
wb = openpyxl.Workbook('part1.xlsx')
for i in range(0,9):
sheets = wb.create_sheet('sheet%d' % (i), i)
wb.save('sam.xlsx')
def sam():
wlb = openpyxl.load_workbook('sam.xlsx')
sheet = wlb.active
for i in range(1,len(list)):
sheet['A%d'%(i+1)] = list[i]
cell = sheet['A%d'%(i+1)]
for i in range(0,len(initSet)):
sheet[str(chr(i+66))+'1'] = initSet[i]
cell = sheet[str(chr(i+66))+'1']
sheet[str(chr(i+66))+'2'] = A[i]
cell = sheet[str(chr(i+66))+'2']
sheet[str(chr(i+66))+'3'] = B[i]
cell = sheet[str(chr(i+66))+'3']
sheet[str(chr(i+66))+'4'] = C[i]
cell = sheet[str(chr(i+66))+'3']
wlb.save('KMU.xlsx')
Part1()
sam()
This is my code, and I want to put the above data in the sheet from 1 to 9.
When I turn the code, only sheet1 has a data value.
Can I know how to use multiple sheets?
I think I can change the sheet=wlb.active
part, what should I do?
Perhaps you are failing to dynamically create multiple worksheets at this stage.
sheets = wb.create_sheet('sheet%d' % (i), i)
When I searched stackoverflow, they said that in order to write create_sheet()
, I have to assign a return value to each new variable. Like ws1
, ws2
, ws3
... In fact, even if you open openpyxl source, the return value of that method is a new ws
(worksheet).
Looking at the source, there is a wb._add_sheet()
method. I think we can use this to bypass it Read the sauce carefully.
© 2024 OneMinuteCode. All rights reserved.