openpyxl.drawing.image/sheet.add_image()
import os
os.chdir('D:\\python_major\\auto_office9')
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')
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')
from openpyxl import load_workbook
from openpyxl.chart import LineChart, Reference
workbook = load_workbook(filename = '简单数据.xlsx')
sheet = workbook.active
chart = LineChart()
data = Reference(worksheet=sheet, min_row=20, max_row=21,min_col=1,max_col=13)
categories = Reference(sheet, min_row=19,min_col=2,max_col=13)
chart.add_data(data,from_rows=True,titles_from_data=True)
chart.set_categories(categories)
sheet.add_chart(chart,'B23')
workbook.save('简单数据.xlsx')
编写一个Python程序,要求
(1)打开文件阿里云天池电商婴儿用户数据.xlsx
(2)统计buy_mount数据
(3)按照buy_mount的数量绘制一个柱状图和饼图
(4)保存该Excel文件
import os
os.chdir('D:\\python_major\\auto_office9')
from openpyxl import Workbook
from openpyxl import load_workbook
from openpyxl.chart import BarChart, Reference
from openpyxl.chart import PieChart
workbook = load_workbook(filename = '阿里云天池电商婴儿用户数据.xlsx')
sheet = workbook.active
workbook_1 = Workbook()
sheet_1 = workbook_1.active
该部分代码完成的功能是:设置程序运行路径、导入相关的库和加载原文件数据和创建新文件表单
cells = sheet['F']#这里就直接使用列索引,不再调用封装的函数了
d = {
}
for cell in cells:
if isinstance(cell.value,int):
d[cell.value] = d.get(cell.value,0) + 1
ls = list(d.items())
ls.sort(key = lambda x:x[1], reverse=True)#从大到小排个序
print(ls) #这种数据格式没有办法写入,这里可以回顾python自动化办公(7),用python列表写入行数据
data = []
for row in ls:
data.append(list(row))
print(data)#转化成这种格式就可以了
for row in data:
sheet_1.append(row)
workbook_1.save(filename='处理数据后的表格.xlsx')
print('数据写入成功!')
这部分注意:字典有个计数的功能这里做一演示,比如一个列表ls为[1,1,1,1,2,2,2,3,3,4,5,6,6,7,8,9],现在要个要求就是统计这个列表里面的数值出现了几次,就可以使用字典的计数功能。代码如下:
ls = [1,1,1,1,2,2,2,3,3,4,5,6,6,7,8,9]
d = {
}
for k in ls:
d[k] = d.get(k,0) + 1
print(d)
–> 输出结果为: {1: 4, 2: 3, 3: 2, 4: 1, 5: 1, 6: 2, 7: 1, 8: 1, 9: 1}
接着就是对应统计数据的写入,显然对于字典数据的写入Excel未有接触过,目前知道可以写入到Excel文件的数据的样式是这样子的(回顾python自动化办公(7),用python列表写入行数据):[ [‘张三’,1], [‘李四’,2],[‘王五’,3], [‘赵六’,4]],所以第一步就是将数据中间的冒号变成’,’,然后再讲小括号变成中括号(小括号–>元祖,中括号–>列表),这两步就对应上面代码的两个print输出
workbook_2 = load_workbook(filename='处理数据后的表格.xlsx')
sheet_2 = workbook_2.active
chart = BarChart()
data = Reference(worksheet=sheet_2, min_row=1, max_row=20,min_col=2,max_col=2)
categories = Reference(sheet_2, min_col=1,min_row=1,max_row=20)
chart.add_data(data,titles_from_data=False)
chart.set_categories(categories)
sheet_2.add_chart(chart,'D2')
print('柱状图绘制成功!')
有点困难在于数据是从哪一行哪一列取,还有标题是不是要默认显示,这些都属于微调
参考网址:https://www.osgeo.cn/openpyxl/charts/pie.html
pie = PieChart()
labels = Reference(sheet_2, min_col=1, min_row=1, max_row=20)
data = Reference(sheet_2, min_col=2, min_row=1, max_row=20)
pie.add_data(data, titles_from_data=False)
pie.set_categories(labels)
sheet_2.add_chart(pie,'M2')
workbook_2.save(filename='处理数据后的表格.xlsx')
print('\n饼图绘制成功!')
要善于自己动手查找相关的文档资料,看一下某一种方法或者模块怎么使用,python的优势就在于可以快速的调用已经完善的包或者模块,当需要的时候就可以针对某一个或者某一些包进行系统的学习,这次的综合练习也有意锻炼大家自己动手的能力
import os
os.chdir('D:\\python_major\\auto_office9')
from openpyxl import Workbook
from openpyxl import load_workbook
from openpyxl.chart import BarChart, Reference
from openpyxl.chart import PieChart
workbook = load_workbook(filename = '阿里云天池电商婴儿用户数据.xlsx')
sheet = workbook.active
workbook_1 = Workbook()
sheet_1 = workbook_1.active
cells = sheet['F']
d = {
}
for cell in cells:
if isinstance(cell.value,int):
d[cell.value] = d.get(cell.value,0) + 1
ls = list(d.items())
ls.sort(key = lambda x:x[1], reverse=True)#从大到小排个序
print(ls) #这种数据格式没有办法写入
data = []
for row in ls:
data.append(list(row))
print(data)#转化成这种格式就可以了
for row in data:
sheet_1.append(row)
workbook_1.save(filename='处理数据后的表格.xlsx')
print('数据写入成功!')
workbook_2 = load_workbook(filename='处理数据后的表格.xlsx')
sheet_2 = workbook_2.active
chart = BarChart()
data = Reference(worksheet=sheet_2, min_row=1, max_row=20,min_col=2,max_col=2)
categories = Reference(sheet_2, min_col=1,min_row=1,max_row=20)
chart.add_data(data,titles_from_data=False)
chart.set_categories(categories)
sheet_2.add_chart(chart,'D2')
print('柱状图绘制成功!')
pie = PieChart()
labels = Reference(sheet_2, min_col=1, min_row=1, max_row=20)
data = Reference(sheet_2, min_col=2, min_row=1, max_row=20)
pie.add_data(data, titles_from_data=False)
pie.set_categories(labels)
sheet_2.add_chart(pie,'M2')
workbook_2.save(filename='处理数据后的表格.xlsx')
print('\n饼图绘制成功!')