向Excel文件中插入图片、生成柱状图、折线图
插入图片
openpyxl.drawing.image/sheet.add_image()
import os
os.chdir('C:\\Users\\Administrator\\Desktop\\test')
from openpyxl import load_workbook
from openpyxl.drawing.image import Image
workbook = load_workbook(filename = '这是一个表格.xlsx')
sheet = workbook.active
photo = Image('1.jpg')
photo.height = 100
photo.width = 220
sheet.add_image(photo,'C1')
workbook.save(filename='这是一个表格.xlsx')
生成图表
插入柱状图 BarChart()/Reference()
import os
os.chdir('C:\\Users\\Administrator\\Desktop\\test')
from openpyxl import load_workbook
from openpyxl.chart import BarChart, Reference
workbook = load_workbook(filename = '简单数据.xlsx')
sheet = workbook.active
chart = BarChart()
data = Reference(worksheet=sheet, min_row=1, max_row=7,min_col=2,max_col=3)
categories = Reference(sheet, min_col=1,min_row=2,max_row=9)
chart.add_data(data,titles_from_data=True)
chart.set_categories(categories)
sheet.add_chart(chart,'E2')
workbook.save('简单数据.xlsx')
来源:CSDN
作者:Python学习中的进阶者
链接:https://blog.csdn.net/weixin_42850424/article/details/104639591