from openpyxl import load_workbook#打开表格
from openpyxl.chart import BarChart, Series, Reference#制作表格
from openpyxl.utils import column_index_from_string#列的序号字母转换成数字(这里好像用不到)
import csv
from openpyxl import load_workbook
import openpyxl
import os #获取路径r
read = r"\10031831_GWM_V3_QNX_CPP_20200420"+".csv"#提取数据的csv文件
write = r"\test"+".xlsx"#作图的xlsx文件
i = 0
j = 0
k = 0
l = 0
m = 0
cwd=os.getcwd()
with open (cwd+r"\excel"+read) as f: #读取csv文件
f_csv = csv.reader(f)
for row in f_csv:
if (row[7] == “Unknown”):#None1
j+=1
elif (row[7] == “Programming”):#None2
k+=1
elif (row[7] == “Advisory”):#None3
l +=1
elif(row[7] == “Required”):#None4
m+=1
elif(row[7] == “None”):#None5
i+=1
filename = (cwd+write)
excel=load_workbook(filename)#打开文件:
table = excel[‘total_report’] #选择操作表
#获取行数和列数:
table[“AB2”] = “Unknown”
table[“AC2”] = j
table[“AB3”] = “Programming”
table[“AC3”] = k
table[“AB4”] = “Advisory”
table[“AC4”] = l
table[“AB5”] = “Required”
table[“AC5”] = m
table[“AB6”] = “None”
table[“AC6”] = i
‘’’ list 形式输入列表我暂时不知道怎么自定义excel网格位置 ‘’’
table[‘AC1’] = ‘Count’
chart = BarChart()
chart.type = “col”#图表方向 col 为竖着 bar为横着
chart.style = 8#柱子颜色
chart.title = "MISRA Category Chrat "#创建标题
chart.y_axis.title = ‘count’#创建Y轴标题
chart.x_axis.title = ‘MISRA Category’#创建X轴标题
#取值的范围 横坐标最左侧 横坐标最右侧 纵坐标最上面 纵坐标最下面
data = Reference(table, min_col = 29,max_col = 29,min_row = 1,max_row =6)
lable = Reference(table, min_col = 28,max_col = 28,min_row = 2,max_row =6)
chart.add_data(data ,titles_from_data=True)#数据带入吧data的第一行作为图例
chart.set_categories(lable)#做下标签
chart.shape = 4
chart.dLbls=openpyxl.chart.label.DataLabelList()#给柱状图加数值
chart.dLbls.showVal=True
table.add_chart(chart, “L2”)#图表左上角保存位置
excel.save(filename)#保存(操作的时候不能打开Excel)