2011-02-16 6 views
4

저는 명함 제작 플로우 (Excel> XML> indesign> 단일 페이지 pdfs)를 마무리하고 있습니다. 파일 이름.일괄 파일 이름 바꾸기 - 목록의 텍스트 삽입 (Python 또는 Java)

내가 지금 무엇을 가지고 :

BusinessCard_01_Blue.pdf 
BusinessCard_02_Blue.pdf 
BusinessCard_03_Blue.pdf (they are gonna go up to the hundreds) 

내가 필요한 것은 (I 쉽게 정규식 이름 목록을 조작 할 수 있습니다) :

BusinessCard_01_CarlosJorgeSantos_Blue.pdf 
BusinessCard_02_TaniaMartins_Blue.pdf 
BusinessCard_03_MarciaLima_Blue.pdf 

나는 자바와 파이썬 유아입니다. 관련 질문을 읽은 후 Automator (Mac) 및 Name Mangler에서이 작업을 시도했지만 작동하지 못했습니다. 사전에

감사합니다, 거스

답변

0

당신은이 검증되지 않은 조각 같은 간다 파이썬에서 파일이 생성됩니다 같은 순서로 이름 목록이있는 경우 :

#!/usr/bin/python 
import os 

f = open('list.txt', 'r') 
for n, name in enumerate(f): 
    original_name = 'BusinessCard_%02d_Blue.pdf' % (n + 1) 
    new_name = 'BusinessCard_%02d_%s_Blue.pdf' % (
          n, ''.join(name.title().split())) 
    if os.path.isfile(original_name): 
     print "Renaming %s to %s" % (original_name, new_name), 
     os.rename(original_name, new_name) 
     print "OK!" 
    else: 
     print "File %s not found." % original_name 
+0

안녕 얘들 아, 훌륭해! 입력에 감사드립니다. 이러한 접근 방식을 이해하는 데는 시간이 걸릴 것이지만 작동 할 것이라고 확신합니다. Paulo가 제안한 프로그램이 나에게 매우 독창적이고 쉬운 것으로 나타났습니다. 다시 고맙습니다. 최고, 거스 – Gus

2

어디 자바에서 이런 일을 할 수있는 올바른 이름에서 볼 수있는지도가 부여 : 같은 것으로

List<Files> originalFiles = ... 
for(File f : originalFiles) { 
    f.renameTo(new File(getNameFor(f))); 
} 

그리고 getNameFor 정의를 : 지도에서

public String getNameFor(File f) { 
    Map<String,String> namesMap = ... 
    return namesMap.get(f.getName()); 
} 

당신은 연결을해야합니다 :

BusinessCard_01_Blue.pdf => BusinessCard_01_CarlosJorgeSantos_Blue.pdf 

의미가 있습니까?

+0

값이 변경되면'renameTo' 메소드가 true를 반환하는 것을 볼 수 있습니다. – OscarRyz

0

파이썬 :

이미 명명 로직을 구현 한 가정을 :

for f in os.listdir(<directory>): 
    try: 
     os.rename(f, new_name(f.name)) 
    except OSError: 
     # fail 

당신은 물론, 문자열 "BusinessCard_01_Blue.pdf"을 받아 문자열을 반환하는 함수 new_name를 작성해야합니다 "BusinessCard_01_CarlosJorgeSantos_Blue.pdf". 파이썬에서

2

(테스트) :

#!/usr/bin/python 
import sys, os, shutil, re 

try: 
    pdfpath = sys.argv[1] 
except IndexError: 
    pdfpath = os.curdir 

employees = {1:'Bob', 2:'Joe', 3:'Sara'} # emp_id:'name' 
files = [f for f in os.listdir(pdfpath) if re.match("BusinessCard_[0-9]+_Blue.pdf", f)] 
idnumbers = [int(re.search("[0-9]+", f).group(0)) for f in files] 
filenamemap = zip(files, [employees[i] for i in idnumbers]) 
newfiles = [re.sub('Blue.pdf', e + '_Blue.pdf', f) for f, e in filenamemap] 

for old, new in zip(files, newfiles): 
    shutil.move(os.path.join(pdfpath, old), os.path.join(pdfpath, new)) 

편집 :이 지금은 아직 변경되지 않은 파일 만 변경합니다.

사전에 employees 사전을 자동으로 작성하려는 경우 알려주세요.