Excel操作
- 相关模块
- openpyxl
- xlrd
- shutil
- 实例
相关模块
openpyxl
可以对.xlsx,.xlsm,.xltx,.xltm文件格式操作
- 打开文件:wb_xlsx = openpyxl.load_workbook(“文件名”)
- 新建文件:wb_xlsx = openpyxl.Workbook()
- 新建sheet表:wb_xlsx_sheet = wb_xlsx.create_sheet(title=“表名”)
- 打开指定sheet表:wb_xlsx_sheet = wb_xlsx.get_sheet_by_name(“表名”)
- 获取表的最大行号:wb_xlsx_sheet._current_row = wb_xlsx_sheet.max_row + 1#当前行指向最大行号+1
- 单元格赋值:wb_xlsx_sheet[‘A1’] = “类别” # A1单元格赋值“类别”
- 行赋值:wb_xlsx_sheet.append((“A”, 1, 2,3))#当前行依次赋值 A 1 2 3
- 保存文件:wb_xlsx.save(“保存名.xlsx”)
xlrd
可以对.et文件读取,高版本的xlrd 也不支持对et访问,因此需要将其版本回退至2.0一下比如:1.2.0
- 打开文件:wb_et = xlrd.open_workbook(“文件名完整路径”, ‘r’)
- 获取sheet数量:wb_et_sheet_len = len(wb_et.sheet_names()) # sheet 数量
- 打开指定sheet表:wb_et_sheet = wb_et.sheets()[sheet_i] # 获取第一张sheet_i表
- 获取表的最大行号:wb_et_nrows = wb_et_sheet.nrows # 获取最大行号
- 读取整行:wb_et_sheet.row_values(row_i)
shutil
shutil 里面有copy函数,用于备份文件
- 备份文件到指定路径:shutil.copy(文件名,路径)# 备份文件
实例
import datetime
import time
import shutil
import pywps
import openpyxl
import os
import xlrd
import AnalyzerMap_Cfg as map_cfg
import win32com.clientdef outputAsExcel(file):# 因为公司加密了.xlsx文件,不能读,因此只能新建# 1、新建一个excel文件# 2、如果.et文件存在则打开,拷贝.et文件所有内容到新的excel中(除Sheet表外)# 3、保存新的.xlsx及.et文件row_offset = 7fileName = "RAM及ROM使用量统计结果"fileName_extension = ".et"fileName_fullPath= os.getcwd() + '\\' + fileName+fileName_extensionfileName_is_exist = 0# 新建excel文件wb_xlsx = openpyxl.Workbook()# 如果存在.et文件,则打开 并 拷贝到新建的.xlsxif os.path.isfile(fileName_fullPath):fileName_is_exist = 1# 打开.et文件wb_et = xlrd.open_workbook(fileName_fullPath, 'r')print(wb_et.sheet_names()) # sheet 名称wb_et_sheet_len = len(wb_et.sheet_names()) # sheet 数量print(wb_et_sheet_len)if 1 == fileName_is_exist:# 如果存在.et文件,则拷贝sheet_i = 0while sheet_i < wb_et_sheet_len:# 逐张sheet表 操作if wb_et.sheet_names()[sheet_i] != "Sheet":wb_et_sheet = wb_et.sheets()[sheet_i] # 获取第一张sheet表wb_xlsx_sheet = wb_xlsx.create_sheet(title=wb_et.sheet_names()[sheet_i])wb_et_nrows = wb_et_sheet.nrows # 获取最大行号# 逐行copyrow_i = 0while row_i < wb_et_nrows:wb_xlsx_sheet.append(wb_et_sheet.row_values(row_i))row_i = row_i + 1#sheet_i = sheet_i + 1if "统计记录" not in wb_xlsx.sheetnames:wb_xlsx_sheet = wb_xlsx.create_sheet(title="统计记录")wb_xlsx_sheet.append(("类别","总量(Kb)","使用量(Kb)","剩余量(Kb)"))else:wb_xlsx_sheet = wb_xlsx.get_sheet_by_name("统计记录")#写入前准备:查找最大行wb_xlsx_sheet._current_row = wb_xlsx_sheet.max_row + 1#指向最大行号+1print("最大行号%d"%(wb_xlsx_sheet._current_row))wb_xlsx_sheet.cell(wb_xlsx_sheet._current_row, 1).value = file # 将分析的文件存在n行1列used_len = round(map_cfg.global_var.totalGlobalROM_A / 1024, 1)total_len = round(int(map_cfg.global_var.Global_ROM_A_Info[2], 10), 1)wb_xlsx_sheet.append(("Global_ROM", total_len, used_len, (total_len - used_len)))used_len = round(map_cfg.global_var.totalLocalRAM / 1024, 1)total_len = round(int(map_cfg.global_var.Local_RAM_Info[2], 10), 1)wb_xlsx_sheet.append(("Local_RAM", total_len, used_len, (total_len - used_len)))used_len = round(map_cfg.global_var.totalLocalRAMSelf / 1024, 1)total_len = round(int(map_cfg.global_var.Local_RAM_Self_Info[2], 10), 1)wb_xlsx_sheet.append(("Local_RAM_Self", total_len, used_len, (total_len - used_len)))used_len = round(map_cfg.global_var.totalGlobalRAM_A / 1024, 1)total_len = round(int(map_cfg.global_var.Global_RAM_A_Info[2], 10), 1)wb_xlsx_sheet.append(("Global_RAM_A", total_len, used_len, (total_len - used_len)))used_len = round(map_cfg.global_var.totalGlobalRAM_B / 1024, 1)total_len = round(int(map_cfg.global_var.Global_RAM_B_Info[2], 10), 1)wb_xlsx_sheet.append(("Global_RAM_B", total_len, used_len, (total_len - used_len)))wb_xlsx.save(fileName+".xlsx")wb_xlsx.save(fileName+".et")return