没有合适的资源?快使用搜索试试~ 我知道了~
首页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


















安全验证
文档复制为VIP权益,开通VIP直接复制

评论0