2017-12-04 1 views
0

openpyxl을 사용하여 Excel의 목록에서 특정 파일을 대량 복사하는 데 도움이됩니다. 폴더 복사, 파일 이름이 목록에 발견되면 특정 파일 이름 접두어 에 대한 워크 시트를 검색 , 특정 접두사로 시작 : 현재 다음되는 과정이파일이없는 경우 파일을 생략하는 방법 - openpyxl, os

#Import required modules 
from openpyxl import load_workbook 
import os 
import shutil 
# Enter source folder 

source = ("C:\\Users\\alec.litchfield\\Desktop\\Resized images\\") 
# Enter destiantion folder - this must exist. 
destination = ("C:\\Users\\alec.litchfield\\Desktop\\Stripped Blackpool Photos\\") 
# Enter the name of your Excel Workbook (including the extension): 
workBookName = ("C:\\Users\\alec.litchfield\\Desktop\\2017.11.22 Point Data - Master.xlsx") #input("Enter the name of your Excel Workbook (including the extension): ") 
workSheetName = ("Point data") #input("Enter the name of the work sheet to read from: ") 
RowRange = int(input ("add total number of rows to check: ")) 
workBook = load_workbook(filename = workBookName, read_only=True,data_only=True) 
workSheet = workBook[workSheetName] 
#filepath = ("C:\\Users\\alec.litchfield\\Desktop\\My Personal Folder\\Python lessons\\Photo copy paste\\") 
for x in range(2, RowRange): 
    file = str (workSheet["j" + str(x)].value) 
      if file.startswith(('P1010','P1020', 'P1070','P1060','P1050' 'P1040' 'P1020')): 
     shutil.copyfile (source + file, destination + file) 

내가 무엇입니까 문제를,이다 파일이 존재하지 않으면 프로세스가 중지됩니다. 이것은 멈춤을 유발하는 데 하나만 걸리고 복사하는 파일의 수를 다루는 경우에는 문제가됩니다. (4000)

파일에 단순히 ' 무시하고 계속 전진하십시오 '

미리 감사드립니다!

알렉

+0

'os.path.isfile (workBookName)' –

답변

0

체크 아웃 파이썬 예외 기능 (마지막 제외하고, 시도) 예를 들어

:

try: 
    #do stuff with the file here 
except FileNotFoundError: 
    #do nothing if the file is not found 
    pass 
관련 문제