使用xlwt 库将数据导入为Excel 表格

背景:之前的需求是,需要统计指定目录中的mp4的时间:

具体代码看如下:(主要是moviepy 库的使用)

   https://www.cnblogs.com/zach0812/p/11277987.html

现在如果更新需求:我们不仅需要统计时长,还要将统计后的数据放到Excel表格中,这我们就要用到xlwt 库了

  1 # -*- coding:utf-8 -*-
  2 # Author :Zcb
  3 
  4 import os
  5 from moviepy.editor import VideoFileClip
  6 import xlwt
  7 
  8 
  9 
 10 file_Dir = u"e:\test" #加个u 是表示unicode 一般用在中文字符前
 11 sum_time =0
 12 
 13 class FileCheck():
 14     def __init__(self):
 15         self.file_dir = file_Dir
 16 
 17     def get_fileSize(self,fileName):
 18         """
 19             获取文件大小
 20         """
 21         file_Byte = os.path.getsize(fileName)
 22         return self.sizeConvert(file_Byte)
 23 
 24     def get_file_Times(self,filename):
 25         """
 26             获取视频时长
 27         """
 28         global sum_time
 29         clip = VideoFileClip(filename)
 30         sum_time +=clip.duration
 31         file_Times = self.timeConvert(clip.duration)
 32         clip.close()
 33         return file_Times
 34     def sizeConvert(self,size): #单位换算
 35         K,M,G = 1024,1024**2,1024**3
 36         if size >=G:
 37             return "{:.3f}G".format(size/G)
 38         elif size >=M:
 39             return "{:.3f}M".format(size/M)
 40         elif size >=K:
 41             return "{:.3f}K".format(size/K)
 42         else:
 43             return "{:.3f}Bytes".format(size)
 44     def timeConvert(self,size): #单位换算
 45         M ,H = 60,60**2
 46         if size <M:
 47             return "{:.3f}s".format(size)
 48         if size <H:
 49             return "{:}m{:.3f}s".format(int(size//M),size%M)
 50         else:
 51             hour = size//H
 52             min = size%H//M
 53             sec = size%H%M
 54             return "{}h{}m{:.3f}s".format(int(hour),int(min),sec)
 55     def get_all_file(self):
 56         """
 57             获取视频下的所有文件
 58         """
 59         ls_file =[]
 60         for root,dirs,files in os.walk(file_Dir):
 61             for file in files:
 62                 if "mp4" in file:
 63                     ls_file.append(os.path.join(root,file))#当前路径下所有非目录子文件
 64         return ls_file
 65 print("============开始,文件较多,请耐心等待...")
 66 fc = FileCheck()
 67 
 68 fc.get_all_file()
 69 files = fc.get_all_file()
 70 #print(files)  files 是放文件全名的列表
 71 data = [["文件名称","文件大小","视频时长"]]
 72 for file in files:
 73     file_size = fc.get_fileSize(file)
 74     file_times= fc.get_file_Times(file)
 75     #print("{} {} {}".format(file,file_size,file_times))
 76     index = file.rfind('\')  #文件名的索引
 77     file_Name =file[index+1:]
 78     #print(file_Name)
 79     cell = []
 80     cell.append(file_Name)
 81     cell.append(file_size)
 82     cell.append(file_times)
 83     data.append(cell)
 84 
 85 print("总时长:{}h{}m{:.3f}s".format(int(sum_time/3600),int(sum_time%3600//60),sum_time%3600%60))
 86 
 87 
 88 wb = xlwt.Workbook(encoding="utf8") #创建工作簿
 89 sheet = wb.add_sheet("data")  # sheet 的名称为data
 90 
 91 #单元格的格式
 92 style = "pattern:pattern solid,fore_colour yellow;"#背景颜色为黄色
 93 style += "font:bold on;" #粗体
 94 style += "align :horz centre,vert center;"#居中
 95 header_style = xlwt.easyxf(style)
 96 
 97 row_count = len(data)
 98 col_count = len(data[0])
 99 for row in range(row_count):
100     for col in range(col_count):
101         if row == 0: #设置表头单元格格式
102             sheet.write(row,col,data[row][col],header_style)
103         else:
104             sheet.write(row,col,data[row][col])
105 wb.save(file_Dir+"\video.xls")
106 print("完成...........")

 注:参考文档:https://blog.csdn.net/xiaomahuan/article/details/78783174

原文地址:https://www.cnblogs.com/zach0812/p/11278317.html