import openpyxl
import numpy as np
wb = openpyxl.Workbook()
hoja1 = wb.get_active_sheet()
hoja1.title = '1'
x = np.linspace(0,100,50)
y = x**2
hoja1.cell(row = 1, column = 1).value = 'x'
hoja1.cell(row = 1, column = 2).value = 'y'
for i in range(0, len(x)):
hoja1.cell(row = i + 2, column = 1).value = x[i]
hoja1.cell(row = i + 2, column = 2).value = y[i]
wb.save('test_save.xlsx')
这是我的代码。它可以保存数据,但是如果我想在同一test_save.xlsx文件中创建另一个工作表,我该怎么办?我试过了,但没有解决:
import openpyxl
import numpy as np
wb = openpyxl.Workbook()
hoja1 = wb.get_active_sheet()
hoja2 = wb.get_active_sheet()
hoja1.title = '1'
hoja2.title = '2'
x = np.linspace(0,100,50)
y = x**2
hoja1.cell(row = 1, column = 1).value = 'x'
hoja1.cell(row = 1, column = 2).value = 'y'
for i in range(0, len(x)):
hoja1.cell(row = i + 2, column = 1).value = x[i]
hoja1.cell(row = i + 2, column = 2).value = y[i]
wb.save('test_save.xlsx')
但这不起作用=(