批量文件重命名–从列表中插入文本(Python 或 Java 中)

发布于 2024-10-17 15:39:35 字数 514 浏览 8 评论 0原文

我正在完成名片制作流程(excel > xml > indesign > 单页 pdf),我想在文件名中插入员工的姓名。

我现在拥有的:

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

我需要的(我可以使用正则表达式轻松操作名单):

BusinessCard_01_CarlosJorgeSantos_Blue.pdf
BusinessCard_02_TaniaMartins_Blue.pdf
BusinessCard_03_MarciaLima_Blue.pdf

我是一个 Java 和 Python 幼儿。我已阅读相关问题,在 Automator (Mac) 和 Name Mangler 中尝试过此操作,但无法使其工作。

提前致谢, 格斯

I'm finishing a business card production flow (excel > xml > indesign > single page pdfs) and I would like to insert the employees' names in the filenames.

What I have now:

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

What I need (I can manipulate the name list with regex easily):

BusinessCard_01_CarlosJorgeSantos_Blue.pdf
BusinessCard_02_TaniaMartins_Blue.pdf
BusinessCard_03_MarciaLima_Blue.pdf

I'm a Java and Python toddler. I've read the related questions, tried this in Automator (Mac) and Name Mangler, but couldn't get it to work.

Thanks in advance,
Gus

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

古镇旧梦 2024-10-24 15:39:35

假设你有一个地图,可以在其中查看正确的名称,你可以在 Java 中执行类似的操作:

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

这有意义吗? ?

Granted you have a map where to look at the right name you could do something like this in Java:

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

And define the getNameFor to something like:

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

In the map you'll have the associations:

BusinessCard_01_Blue.pdf => BusinessCard_01_CarlosJorgeSantos_Blue.pdf

Does it make sense?

忆沫 2024-10-24 15:39:35

在 Python 中(已测试):

#!/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 字典的东西,请告诉我。

In Python (tested):

#!/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))

EDIT: This now alters only those files that have not yet been altered.

Let me know if you want something that will build the the employees dictionary automatically.

醉城メ夜风 2024-10-24 15:39:35

如果你有一个与文件生成顺序相同的名称列表,在 Python 中它就像这个未经测试的片段:

#!/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

If you have a list of names in the same order the files are produced, in Python it goes like this untested fragment:

#!/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
(り薆情海 2024-10-24 15:39:35

Python:

假设您已经实现了命名逻辑:

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

当然,您需要编写一个函数 new_name ,它接受字符串 "BusinessCard_01_Blue.pdf" 并返回该字符串“BusinessCard_01_CarlosJorgeSantos_Blue.pdf”

Python:

Assuming you have implemented the naming logic already:

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

You will, of course, need to write a function new_name which takes the string "BusinessCard_01_Blue.pdf" and returns the string "BusinessCard_01_CarlosJorgeSantos_Blue.pdf".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文