import pandas as pd
import numpy as np
from openpyxl import load_workbook
path = "1.xlsx"
book = load_workbook(path)
sheetnamelsit=book.sheetnames
print(sheetnamelsit)
writer = pd.ExcelWriter(path, engine='openpyxl')
writer.book = book
x3 = np.random.randn(100, 2)
df3 = pd.DataFrame(x3)
x4 = np.random.randn(100, 2)
df4 = pd.DataFrame(x4)
df3.to_excel(writer, sheet_name = 'x3')
if 'Sheet' in sheetnamelsit:
del book['Sheet']
df4.to_excel(writer, sheet_name = 'Sheet')
else:
df4.to_excel(writer, sheet_name='Sheet')
writer.save()
writer.close()
像版主说的,加个判断不就好了,要覆盖原表,删了再新建不就可以么 |