python脚本自动统计excel_python脚本实现-excel二级统计
pandas和SQL數據分析實戰視頻教程
統計excel ?C列 化學 ?和D列 補充申請 ?兩個條件藥品數量
程序優勢在于批量統計,把參數寫到excel里,行為單位
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 7 08:38:51 2016
content內容必須是逗號分隔,例如'化藥,中藥'
用于統計excel--sheet---二級條件篩選的數據統計
算法:
1.讀取所有excel內容,保存到二維列表中
2.二級篩選
@author: Administrator
"""
import csv,xlrd,os,openpyxl
#獲取所有excel文件名
def Get_excelFileNames():
excelFiles_list=[]
for excelFilename in os.listdir('.'):
if excelFilename.endswith('.xlsx') or excelFilename.endswith('.xls'):
excelFiles_list.append(excelFilename)
return excelFiles_list
#獲取一個excel內首頁表格
def Get_sheet1_from_oneExcel(excelFileName):
wb=xlrd.open_workbook(excelFileName)
sheet=wb.sheets()[0]
return sheet
#獲取excel的首頁信息,去除首行
def Get_sheet1_Data_NoFirstLine(excelFileName):
sheet=Get_sheet1_from_oneExcel(excelFileName)
#獲取表格行數和列數
highest_row=sheet.nrows
highest_column=sheet.ncols
sheet_data=[]
for rowNum in range(1,highest_row):
#獲取行數據
rowData=sheet.row_values(rowNum)
sheet_data.append(rowData)
return sheet_data
#把csv文檔內容轉換成一個列表,不包括首行
def Read_CsvRows_noFirstLine(csvFilename):
csvRows = []
csvFileObj = open(csvFilename)
readerObj = csv.reader(csvFileObj)
for row in readerObj:
if readerObj.line_num == 1:
continue # skip first row
csvRows.append(row)
csvFileObj.close()
return csvRows
def one_excel_column_statistic(csv_row):
#csv特定行包含excel名字,sheet名,列,搜索內容
excelFilename=csv_row[0]
#print("excelFilename",excelFilename)
sheetName=csv_row[1]
#print("sheetName",sheetName)
column=int(csv_row[2])
#print("column",column)
content=csv_row[3]
#'化藥,中藥'必須轉換為['化藥', '中藥'],否則無法遍歷
content_list=content.split(",")
wb=xlrd.open_workbook(excelFilename)
sheet=wb.sheet_by_name(sheetName)
#獲取excel指定列的類容
column_content_list=sheet.col_values(column)
#print("column_content_list",column_content_list)
number=0
for i in content_list:
count=column_content_list.count(i)
number+=count
print("excel文件名",excelFilename)
print("統計內容:",content)
print("數量:",number)
print("-"*20)
return number
def all_excel_column_statistic(csvFileName):
csv_content=Read_CsvRows_noFirstLine(csvFileName)
for csv_row in csv_content:
one_excel_column_statistic(csv_row)
def second_condition_statistic(csv_row):
count=0
excelFileName=csv_row[0]
sheet_data=Get_sheet1_Data_NoFirstLine(excelFileName)
Condition1=csv_row[2].split(',')[1]
Column1_letter=(csv_row[2].split(',')[0])
index1=column_letter_index_convert(Column1_letter)
Condition2=csv_row[3].split(',')[1]
Column2_letter=(csv_row[3].split(',')[0])
index2=column_letter_index_convert(Column2_letter)
for rowData in sheet_data:
if rowData[index1]==Condition1 and rowData[index2]==Condition2:
count+=1
print("excel文件名",excelFileName)
print("統計內容:",Condition1+">"+Condition2)
print("數量:",count)
print("-"*20)
return count
def all_second_condition_statistic(csvFileName):
csvRows=Read_CsvRows_noFirstLine(csvFileName)
for csv_row in csvRows:
second_condition_statistic(csv_row)
#excel列的
def column_letter_index_convert(letter):
list_letter_index=[["A",0],["B",1],["C",2],["D",3],["E",4],["F",5],["G",6],["H",7],["I",8],\
["J",9],["K",10],["L",11],["M",12],["N",13],["O",14],["P",15],["Q",16],["R",17],\
["S",18],["T",19],["U",20],["V",21],["W",22],["X",23],["Y",24],["Z",25]]
for i in list_letter_index:
if i[0]==letter.upper():
index=i[1]
return index
csvFileName="config.csv"
all_second_condition_statistic(csvFileName)
'''
excelFileName='1月.xlsx'
csv_row=['1月.xlsx', 'Sheet1', 'C,化藥', '3,補充申請']
'''
print("問題反饋郵箱:231469242@qq.com")
print(" .-. ")
print(" \ \ ")
print(" \ \ ")
print(" 喵 喵 喵 | | ")
print(" | | ")
print(" /\---/\ _,---._ | | ")
print(" /^ ^ \,' `. ; ")
print(" ( O O ) ; ")
print(" `.=o=__,' \ ")
print(" / _,--.__ \ ")
print(" / _ ) ,' `-. `-. \ ")
print(" / ,' / ,' \ \ \ \ ")
print(" / / / ,' (,_)(,_) ")
print("(,; (,,) ")
var=input("enter anykey to quit")
總結
以上是生活随笔為你收集整理的python脚本自动统计excel_python脚本实现-excel二级统计的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 现代软件工程讲义 12 绩效管理
- 下一篇: 逻辑回归python正则化 选择参数_吴