是否有一种方法可以通过匹配Excel文件列在文件夹中重命名文件名?

发布于 2025-02-13 10:41:17 字数 1291 浏览 2 评论 0原文

例如,我有一个文件pdf,只是约翰·史密斯(John Smith)。在Excel文件中,我有约翰·史密斯(John Smith),在另一栏中,约翰·史密斯(John Smith)的税号。我如何获得约翰·史密斯5555555?想从匹配excel file.def rename_file(file_to_to_rename,source_file)中的名称中重命名所有文件名:

def rename_file(file_to_rename, source_file):
p = Path('C:\Users\Chris\Box\Capital\Fund, 2 L.P\Tax')
filename = p.stem
wb = xlrd.open_workbook('C:\Users\Chris\Box\Capital\Fund, 2 L.P\Tax\Fund 2 LP - Document Uploader ID 2021')
sheet = wb.sheet_by_index(0)

for row_num in range(sheet.nrows):
    row_value = sheet.row_values(row_num)
    col = 4 # 'john smith' col number
    if row_value[col] == filename:
        new_filename = f'{row_value[col]}_{row_value[col - 1]}'  # format as you want
        p.rename(Path(p.parent, new_filename + p.suffix))  # rename
        break

def get_paths_in_directory(directory):
    return Path(directory).glob('*.pdf')

if __name__ == "__main__":

source_file = "Fund 2 LP - Document Uploader ID 2021.xlsx" # excel file to get new filename
source_directory = "C:\Users\Chris\Box\Capital\Fund, 2 L.P\Tax\2021 Final" # directory where your files to rename are.

# iterate all pdf files in the given directory
paths = get_paths_in_directory(source_directory)
for file_to_rename in paths:
    rename_file(str(file_to_rename), source_file)

Example would be I have a file PDF and it's just John Smith. In the Excel file I have John Smith and in another column John Smith's Tax ID number. How do I get John Smith 5555555? Would like to rename all file names from matching the name in an Excel file.def rename_file(file_to_rename, source_file):

def rename_file(file_to_rename, source_file):
p = Path('C:\Users\Chris\Box\Capital\Fund, 2 L.P\Tax')
filename = p.stem
wb = xlrd.open_workbook('C:\Users\Chris\Box\Capital\Fund, 2 L.P\Tax\Fund 2 LP - Document Uploader ID 2021')
sheet = wb.sheet_by_index(0)

for row_num in range(sheet.nrows):
    row_value = sheet.row_values(row_num)
    col = 4 # 'john smith' col number
    if row_value[col] == filename:
        new_filename = f'{row_value[col]}_{row_value[col - 1]}'  # format as you want
        p.rename(Path(p.parent, new_filename + p.suffix))  # rename
        break

def get_paths_in_directory(directory):
    return Path(directory).glob('*.pdf')

if __name__ == "__main__":

source_file = "Fund 2 LP - Document Uploader ID 2021.xlsx" # excel file to get new filename
source_directory = "C:\Users\Chris\Box\Capital\Fund, 2 L.P\Tax\2021 Final" # directory where your files to rename are.

# iterate all pdf files in the given directory
paths = get_paths_in_directory(source_directory)
for file_to_rename in paths:
    rename_file(str(file_to_rename), source_file)

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

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

发布评论

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

评论(1

你穿错了嫁妆 2025-02-20 10:41:17

这样的事情会起作用。 (您需要在安装软件包中安装),

import xlrd
from pathlib import Path


def rename_file(file_to_rename, source_file):
    p = Path(file_to_rename)
    filename = p.stem
    wb = xlrd.open_workbook(source_file)
    sheet = wb.sheet_by_index(0)

    for row_num in range(sheet.nrows):
        row_value = sheet.row_values(row_num)
        col = 0 # 'john smith' col number
        if row_value[col] == filename:
            new_filename = f'{row_value[col]}_{row_value[col + 1]}'  # format as you want
            p.rename(Path(p.parent, new_filename + p.suffix))  # rename
            break


if __name__ == "__main__":
    # add iteration to rename multiple files
    file_to_rename = "john.pdf"
    source_file = "new_names.xlsx"
    rename_file(file_to_rename, source_file)

如果要重命名多个文件,则可以添加迭代。
希望这有帮助!

-

编辑

由于您索要更多详细信息,我将添加一个简单的迭代示例。

import xlrd
from pathlib import Path


def rename_file(file_to_rename, source_file):
    p = Path(file_to_rename)
    filename = p.stem
    wb = xlrd.open_workbook(source_file)
    sheet = wb.sheet_by_index(0)

    for row_num in range(sheet.nrows):
        row_value = sheet.row_values(row_num)
        col = 0 # 'john smith' col number
        if row_value[col] == filename:
            new_filename = f'{row_value[col]}_{row_value[col + 1]}'  # format as you want
            p.rename(Path(p.parent, new_filename + p.suffix))  # rename
            break

def get_paths_in_directory(directory):
    return Path(directory).glob('*.pdf')

if __name__ == "__main__":

    source_file = "new_names.xlsx" # excel file to get new filename
    source_directory = "files_to_rename" # directory where your files to rename are.
    
    # iterate all pdf files in the given directory
    paths = get_paths_in_directory(source_directory)
    for file_to_rename in paths:
        rename_file(str(file_to_rename), source_file)

而且,当然,创建您的脚本文件(例如'rename_files.py'),复制并粘贴此代码,并通过python3 rename_files.py运行它,

让我知道您是否有更多问题。

Something like this will work. (You need to pip install the packages)

import xlrd
from pathlib import Path


def rename_file(file_to_rename, source_file):
    p = Path(file_to_rename)
    filename = p.stem
    wb = xlrd.open_workbook(source_file)
    sheet = wb.sheet_by_index(0)

    for row_num in range(sheet.nrows):
        row_value = sheet.row_values(row_num)
        col = 0 # 'john smith' col number
        if row_value[col] == filename:
            new_filename = f'{row_value[col]}_{row_value[col + 1]}'  # format as you want
            p.rename(Path(p.parent, new_filename + p.suffix))  # rename
            break


if __name__ == "__main__":
    # add iteration to rename multiple files
    file_to_rename = "john.pdf"
    source_file = "new_names.xlsx"
    rename_file(file_to_rename, source_file)

You can add iteration if you want to rename multiple files.
Hope this helps!

--

Edit

Since you asked for more details, I'll add a simple iteration example.

import xlrd
from pathlib import Path


def rename_file(file_to_rename, source_file):
    p = Path(file_to_rename)
    filename = p.stem
    wb = xlrd.open_workbook(source_file)
    sheet = wb.sheet_by_index(0)

    for row_num in range(sheet.nrows):
        row_value = sheet.row_values(row_num)
        col = 0 # 'john smith' col number
        if row_value[col] == filename:
            new_filename = f'{row_value[col]}_{row_value[col + 1]}'  # format as you want
            p.rename(Path(p.parent, new_filename + p.suffix))  # rename
            break

def get_paths_in_directory(directory):
    return Path(directory).glob('*.pdf')

if __name__ == "__main__":

    source_file = "new_names.xlsx" # excel file to get new filename
    source_directory = "files_to_rename" # directory where your files to rename are.
    
    # iterate all pdf files in the given directory
    paths = get_paths_in_directory(source_directory)
    for file_to_rename in paths:
        rename_file(str(file_to_rename), source_file)

And, of course, create your script file (e.g. 'rename_files.py'), copy and paste this code, and run it by python3 rename_files.py

let me know if you got more questions.

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