【python办公自动化(6期)】9.向Excel文件中插入图片、生成柱状图、折线图和饼图

南笙酒味 提交于 2020-03-04 00:14:20

向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')
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!