import pandas as pd import numpy as np path = r"D:\excelData\data.xlsx" x1 = np.random.randn(100, 2) df1 = pd.DataFrame(x1) x2 = np.random.randn(100, 2) df2 = pd.DataFrame(x2) writer = pd.ExcelWriter(path, engine = 'xlsxwriter') df1.to_excel(writer, sheet_name = 'x1') df2.to_excel(writer, sheet_name = 'x2') writer.save() writer.close()
或者
import pandas as pd import numpy as np from openpyxl import load_workbook path = r"D:\excelData\data.xlsx" book = load_workbook(path) 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') df4.to_excel(writer, sheet_name = 'x4') writer.save() writer.close()
或者
import pandas as pd from openpyxl import load_workbook from openpyxl.utils.dataframe import dataframe_to_rows fileLocation =r'D:\excelData\data.xlsx' writeLocation=r'D:\excelData\dataNew.xlsx' data = {'Name':['C','Java','Python'],'Age':[32,43,34],'Salary':[20000,34000,32000]} df = pd.DataFrame(data) book = load_workbook(fileLocation) sheet = book.create_sheet("Sheet Name") for row in dataframe_to_rows(df, index=False, header=True): sheet.append(row) book.save(writeLocation)