插入图片
- 先建立图片对象 openpyxl.drawing.image.Image(图片文件)
- 工作表.add_image(图片对象,单元格)
from openpyxl import *
file = r"D:\xlsx\lianxi_5.xlsx"
wb1 = load_workbook(file)
ws1 = wb1.active
image1 = drawing.image.Image(r"D:\xlsx\dog.png")
image1.height = 100
image1.width = 100
ws1.add_image(image1)
wb1.save(file)
绘制折线图
- 先建立折线图对象,设置y轴和x轴的数据,data为作图数据,ref为横坐标数据。
- 设置x,y轴,title等。ct1加入data。
- 设置每个序列的作图格式,线性,标记,颜色等。
- ct1设置横轴序列
- add_chart。
from openpyxl import *
file = r"D:\xlsx\lianxi_5.xlsx"
wb1 = load_workbook(file)
ws1 = wb1.active
ct1 = chart.LineChart()
data = chart.Reference(ws1, min_row=1, max_row=5, min_col=2, max_col=4)
ref = chart.Reference(ws1, min_row=2, max_row=5, min_col=1)
ct1.title = "成绩图"
ct1.x_axis.title = "姓名"
ct1.y_axis.title = "分数"
ct1.add_data(data, titles_from_data=True)
s0 = ct1.series[0]
s0.marker.symbol = "triangle"
s0.marker.graphicalProperties.solidFill = "FF0000"
s0.marker.graphicalProperties.line.solidFill = "FF0000"
s1 = ct1.series[1]
s1.marker.symbol = "dot"
s1.marker.graphicalProperties.solidFill = "00FF00"
s1.marker.graphicalProperties.line.DashStyle = "sysDot"
s2 = ct1.series[2]
s2.smooth = True
ct1.set_categories(ref)
ws1.add_chart(ct1, "C5")
wb1.save(file)