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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章