处理Excel的Python算法_6_:xlwings与其他模块交互
程序员文章站
2024-02-23 23:34:30
...
xlwings 与pandas交互
import xlwings as xw
import pandas as pd
app = xw.App(visible=False)
workbook = app.books.add()
worksheet = workbook.sheets.add('新工作表')
df = pd.DataFrame([[1, 2], [3, 4]], columns=['a', 'b'])
worksheet.range('A1').value = df
workbook.save(r'D:\运行文件\table.xlsx')
workbook.close()
app.quit()
xlwings与Matplotlib交互
import xlwings as xw
import matplotlib.pyplot as plt
figure = plt.figure()
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.plot(x, y)
app = xw.App(visible=False)
workbook = app.books.add()
worksheet = workbook.sheets.add('新工作表')
# 将绘制的图表写入工作簿
worksheet.pictures.add(figure, name='图片1', update=True, left=100)
workbook.save(r'D:\运行文件\table.xlsx')
workbook.close()
app.quit()