介绍
这篇文章主要讲解了如何实现Python openpyxl插入折线图,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。
我就废话不多说了,还是直接看代码吧!
导入操作系统 进口openpyxl 从datetime进口日期 从openpyxl进口工作簿 从openpyxl。表导入( 系列中, LineChart, 参考, ) def add_line_chart(标题、wss min_col、min_row max_col, max_row): c1=LineChart () c1。title=#图的标题 c1。风格=12 #线条的风格 c1.y_axis。title=& # 39; % # 39;# y坐标的标题 如果& # 39;idc # 39;在标题: c1.x_axis。number_format=& # 39; d-mmm& # 39;#规定日期格式这是月,年格式 c1.x_axis。majorTimeUnit=癕onths"#规定日期间隔注意天;几个月大写 c1.x_axis。title=癉ate"# x坐标的标题 data=https://www.yisu.com/zixun/Reference (wss min_col=min_col min_row=min_row max_col=max_col max_row=max_row) #图像的数据起始行,起始列,终止行,终止列 c1。add_data(数据、titles_from_data=True from_rows=True) 日期=参考(wss min_col=2, min_row=1, max_col=max_col) c1.set_categories(日期) wss。add_chart (c1, A6) #将图表添加到表中 def save_data_to_excel (file_name target_sheet_name): select_cursor=connect.cursor () select_sql="选择phone_company、record_date record_percent从phone_statistics record_company=' % s ' " % target_sheet_name 如果target_sheet_name==癐DC”: select_sql="选择phone_company、record_q record_percent从phone_statistics record_company=' % s ' " % target_sheet_name select_cursor。执行(select_sql ()) data_dic={} all_date={} all_phone_company={} 在select_cursor:项 如果target_sheet_name==癐DC”: data_dic[项目[0]+“_”+项目[1]]=[2]项 其他: 如果类型([1])项==str: data_dic[项目[0]+“_”+项目[1]]=[2]项 其他: data_dic[项目[0]+“_”+项目[1].strftime (“Y % - % - % d”)]=[2]项 all_date[项目[1]]=1 all_phone_company项[0]=1 如果os.path.exists (file_name): 世行=openpyxl.load_workbook (file_name) 其他: 世行=工作簿() 试一试: wb.remove_sheet (wb['单']) 除了例外e: 通过 试一试: wb.remove_sheet (wb (target_sheet_name)) 除了例外e: 通过 试一试: 表=wb (target_sheet_name) 除了例外e: 表=wb.create_sheet () start_date_index=' B ' 在all_date.keys each_date (): 如果target_sheet_name==癐DC”: 表[' % s1 ' % start_date_index]=each_date 其他: 如果类型(each_date)==str: 表[' % s1 ' % start_date_index]=each_date 其他: 表[' % s1 ' % start_date_index]=each_date.strftime (“Y % - % - % d”) start_date_index=科(奥德(start_date_index) + 1) start_name_index=2 在all_phone_company.keys each_name (): 表[' % d ' % start_name_index]=each_name start_name_index +=1 start_date_index=' B ' start_name_index=2 在all_date.keys each_date (): 在all_phone_company.keys each_name (): 如果target_sheet_name==癐DC”: 关键=each_name +“_”+ each_date 如果在data_dic关键: 表[' % s % d ' % (start_date_index start_name_index)]=data_dic(例子) 其他: 如果类型(each_date)==str: 关键=each_name +“_”+ each_date 其他: 关键=each_name +“_”+ each_date.strftime (“Y % - % - % d”) 如果在data_dic关键: 表[' % s % d ' % (start_date_index start_name_index)]=data_dic(例子) start_name_index +=1 start_date_index=科(奥德(start_date_index) + 1) start_name_index=2 表。title=target_sheet_name sheet.column_dimensions [A]。宽度=20 start_date_index=' B ' 在all_date.keys each_date (): sheet.column_dimensions [start_date_index]。宽度=13 start_date_index=科(奥德(start_date_index) + 1) add_line_chart (target_sheet_name.upper() +”的电话统计”表,1,2,len (all_date.keys ()) + 1, 15分钟(len (all_phone_company.keys ()) + 1)) wb.save (file_name) 通过
<强> Python情节折线图折线图强>
我就废话不多说了,还是直接看代码吧!
# 1折线图数据 # trace1 -基本格式 # trace2 -更多参数 trace1=go.Scatter ( x=x1, y=y2, ) trace2=go.Scatter ( x=x2, y=y2, 模式=& # 39;行# 39;,#模式:行线,标记点。可用“+”相连 name=& # 39;么# 39;,#折线名,显示于图例 connectgaps=True #连接缺失点两端默认假 行=dict类型( 颜色=(& # 39;rgb(205、12、24) & # 39;), #颜色 宽度=4 #线宽 dash=& # 39;短跑# 39;)#虚线:一冲一,····,dashdot一·一 ) ) # 2打包数据 data=https://www.yisu.com/zixun/[trace1 trace2] # 3格式 布局=dict (title=罢巯摺? xaxis=dict (title='时间”),#横轴坐标 桠溪=dict (title=笆俊?,#总轴坐标 传说=dict (x=1.1, y=1) #图例位置 ) # 4打包数据+格式 无花果=dict (data=数据布局(布局) # 5画图 py。iplot(图文件名=styled-line)如何实现Python openpyxl插入折线图