首页 > 解决方案 > 如何编辑多个excel表的名称?

问题描述

我有一组文件夹名称作为播放器的名称。在文件夹中,我有一个名为 gw 的 excel 表或两个名为 gw 和 history 的 excel 表。

现在,我希望将所有 gw excel 工作表的名称更改为它们所属文件夹的名称。

这是一个示例:我有一个文件夹名称“Aaron Cresswell”。在文件夹中,我有两个 excel 表“gw”和“history”。我希望将“gw”excel 表的名称更改为“Aaron Cresswell”。

我不能手动操作,因为有 439 个这样的播放器文件夹。

任何帮助将不胜感激。谢谢。

标签: pythondataframe

解决方案


你可以在 Python 中这样做。

import os
base_path = r'C:\Users\Documents\<snip>'
for directory in os.listdir(base_path):
    os.rename(
             os.path.join(base_path, directory, 'gw.xlsx'), 
             os.path.join(base_path, directory, '{}.xlsx'.format(directory))
             )

wherebase_path是包含播放器名称目录的文件夹的路径。


推荐阅读