没有合适的资源?快使用搜索试试~ 我知道了~
首页Python openpyxl 插入折线图实例
资源详情
资源评论
资源推荐
Python openpyxl 插入折线图实例插入折线图实例
主要介绍了Python openpyxl 插入折线图实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
我就废话不多说了,还是直接看代码吧!
import os
import openpyxl
from datetime import date
from openpyxl import Workbook
from openpyxl.chart import (
Series,
LineChart,
Reference,
)
def add_line_chart(title, wss, min_col, min_row, max_col, max_row):
c1 = LineChart()
c1.title = title # 图的标题
c1.style = 12 # 线条的style
c1.y_axis.title = 'percent' # y坐标的标题
if 'IDC' not in title:
c1.x_axis.number_format = 'd-mmm' # 规定日期格式 这是月,年格式
c1.x_axis.majorTimeUnit = "Months" # 规定日期间隔 注意days;Months大写
c1.x_axis.title = "Date" # x坐标的标题
data = Reference(wss, min_col=min_col, min_row=min_row, max_col=max_col, max_row=max_row) # 图像的数据 起始行、起始列、终止行、终止列
c1.add_data(data, titles_from_data=True, from_rows=True)
dates = Reference(wss, min_col=2, min_row=1, max_col=max_col)
c1.set_categories(dates)
wss.add_chart(c1, "A6") # 将图表添加到 sheet中
def save_data_to_excel(file_name, target_sheet_name):
select_cursor = connect.cursor()
select_sql = "select phone_company, record_date, record_percent from phone_statistics where record_company = '%s'" % target_sheet_name
if target_sheet_name == "IDC":
select_sql = "select phone_company, record_q, record_percent from phone_statistics where record_company = '%s'" % target_sheet_name
select_cursor.execute(select_sql, ())
data_dic = {}
all_date = {}
all_phone_company = {}
for item in select_cursor:
if target_sheet_name == "IDC":
data_dic[item[0] + '_' + item[1]] = item[2]
else:
if type(item[1]) == str:
data_dic[item[0] + '_' + item[1]] = item[2]
else:
data_dic[item[0] + '_' + item[1].strftime("%Y-%m-%d")] = item[2]
all_date[item[1]] = 1
all_phone_company[item[0]] = 1
if os.path.exists(file_name):
wb = openpyxl.load_workbook(file_name)
else:
wb = Workbook()
try:
wb.remove_sheet(wb['Sheet'])
except Exception as e:
pass
try:
wb.remove_sheet(wb[target_sheet_name])
except Exception as e:
pass
try:
sheet = wb[target_sheet_name]
except Exception as e:
sheet = wb.create_sheet()
start_date_index = 'B'
for each_date in all_date.keys():
if target_sheet_name == "IDC":
sheet['%s1' % start_date_index] = each_date
else:
if type(each_date) == str:
sheet['%s1' % start_date_index] = each_date
else:
sheet['%s1' % start_date_index] = each_date.strftime("%Y-%m-%d")
start_date_index = chr(ord(start_date_index) + 1)
start_name_index = 2
weixin_38553837
- 粉丝: 3
- 资源: 954
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- zigbee-cluster-library-specification
- JSBSim Reference Manual
- c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf
- 建筑供配电系统相关课件.pptx
- 企业管理规章制度及管理模式.doc
- vb打开摄像头.doc
- 云计算-可信计算中认证协议改进方案.pdf
- [详细完整版]单片机编程4.ppt
- c语言常用算法.pdf
- c++经典程序代码大全.pdf
- 单片机数字时钟资料.doc
- 11项目管理前沿1.0.pptx
- 基于ssm的“魅力”繁峙宣传网站的设计与实现论文.doc
- 智慧交通综合解决方案.pptx
- 建筑防潮设计-PowerPointPresentati.pptx
- SPC统计过程控制程序.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0