是否有一种方法可以通过匹配Excel文件列在文件夹中重命名文件名?
例如,我有一个文件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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这样的事情会起作用。 (您需要在安装软件包中安装),
如果要重命名多个文件,则可以添加迭代。
希望这有帮助!
-
编辑
由于您索要更多详细信息,我将添加一个简单的迭代示例。
而且,当然,创建您的脚本文件(例如'rename_files.py'),复制并粘贴此代码,并通过
python3 rename_files.py
运行它,让我知道您是否有更多问题。
Something like this will work. (You need to pip install the packages)
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.
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.