14、python 对EXCEL操作

基本问题描述:Python对指定的excel文件的字段进行进行判别,并提取有用信息建立新的excel;

表如下:对A B的“实际”字段进行非0选择~~ 若同时非0 ;进行视频保留和xls数据提取;

# -*- coding: utf-8 -*-
from openpyxl import load_workbook
import shutil
import os
import xlwt

source="/home/###"
destion="/home/###/###"

workbook = load_workbook(u'toinfo.xlsx',data_only=True)   
booksheet = workbook.active               

rows = booksheet.rows

columns = booksheet.columns

xls_data=[]
mp4_list=[]
i =  0
s =  0
t_b= 0
# 迭代所有的行 0 3 6    1 4 7   2 5 8 
for row in rows:
    i = i + 1
    line = [col.value for col in row]
    cell_data=[]
    for col in range(2,12):
       cell_data.append(booksheet.cell(row=i, column=col).value) 
    if cell_data[0]:
       xls_data.append(cell_data)

book=xlwt.Workbook(encoding="utf-8",style_compression=0)
sheet = book.add_sheet('info_data', cell_overwrite_ok=True)
k=0
for data_id,data_item in enumerate(xls_data):
     if data_id%3!=0 and data_item[1]>0 and (data_id+1)%3==0:# 只要接触和购买都存在的
           print (xls_data[data_id-2],xls_data[data_id-1],xls_data[data_id])
           mp4_list.append(xls_data[data_id-2][0].strip(' '))
           for x in range(0,3):     
               for y in range(0,len(xls_data[data_id+x-2])):
                   #print (xls_data[data_id+x-2][y])
                   sheet.write(k,y,xls_data[data_id+x-2][y])
               k=k+1          

for mp4_id,mp4_data in enumerate(mp4_list):
    shutil.copy(source+"/"+mp4_data, destion)

###  上面是完成视频中视频 提取过程
'''
t=0
book=xlwt.Workbook(encoding="utf-8",style_compression=0)
sheet = book.add_sheet('info_data', cell_overwrite_ok=True)
for info_id,info in enumerate(xls_data):
    if info_id%3!=0 and info[1]>0:  
       for info_item_id,info_item in enumerate(info):
          sheet.write(t,info_item_id,info_item)
    t=t+1
'''
book.save(destion+'/'+'test1.xlsx')     


# 上面代码是 
# 0 3 6
# 1 4 7
# 2 5 8

猜你喜欢

转载自blog.csdn.net/sxj731533730/article/details/101425999