Python脚本根据excel单元格移动文件

时间:2016-02-18 17:07:03

标签: python excel scripting

我想将文件从一个位置复制到另一个位置。我有包含数据的excel文件。例如,如果数据为“是”,我想将其移动到另一个位置,如果是“no”或“pending”,则不要复制它。

这是我到目前为止所做的。请指教:

#Import Openpyxl and Shutil 
import openpyxl
import shutil

#Read a File (Excel File Must Be In Working Directory or Change Location)    
wb = openpyxl.load_workbook('OfferDecision.xlsx')
sheet = wb.get_sheet_by_name('Sheet1')

for colOfCell in sheet['G2':'G158']:
    for cellObj in colOfCell:
       if sheet[cellObj.value] == "Yes":
            shutil.copytree(a\b, c)

1 个答案:

答案 0 :(得分:1)

我有一个看起来像这样的excel文件

Y/N    File Name
Yes   LeoQ1.csv
No    LeoQ2.csv
Yes   LeoQ3.csv
No    LeoQ4.csv

这里是将B列文件复制到变量dst中指定的目标目录的代码。它根据A列中的是/否复制文件。

import openpyxl
import shutil
src = "C:\\Users\\martbar\\Desktop\\"
dst = "C:\\Users\\martbar\\Documents\\"
exFile = "C:\\Users\\martbar\\Desktop\\test.xlsx"

wb = openpyxl.load_workbook(exFile)
ws = wb.get_sheet_by_name('Sheet1')

for row in ws.iter_rows(row_offset=1):
    if row[0].value == "Yes":
        file = src+row[1].value
        fileDst = dst+row[1].value
        shutil.copyfile(file, fileDst)