python_xecel

  • 移动并重命名工作簿
 1 from pathlib import Path  # 导入pathlib模块的path类
 2 import time
 3 
 4 # Press the green button in the gutter to run the script.
 5 if __name__ == '__main__':
 6     data = time.strftime('%y-%m-%d %H:%M:%S')
 7 
 8     old_file_path = Path('D:\\demo\\python\\test_excel\\file_from\\test.xlsx')        # 这里的Path,等价于os.path,join();参考第10行代码
 9     new_file_path = Path('D:\\demo\\python\\test_excel\\file_to\\test_rename.xlsx')
10     # test_path = Path('D:', 'demo', 'python', 'test_excel', 'file_from', 'test.xlsx')
11     old_file_path.rename(new_file_path)                               # 剪切并重命名
12 
13     if new_file_path.is_file():
14         print(f'{data}:{old_file_path} rename successed')
  •  解析工作簿的路径信息
 1 from pathlib import Path  # 导入pathlib模块的path类
 2 import time
 3 
 4 # Press the green button in the gutter to run the script.
 5 if __name__ == '__main__':
 6     data = time.strftime('%y-%m-%d %H:%M:%S')
 7     file_path = Path('D:\\demo\\python\\test_excel\\file_to\\test_rename.xlsx')
 8     # 解析工作簿的路径信息
 9     path = file_path.parent
10     # 打印文件所在路径
11     print(f'文件路劲----:{path}')
12     file_name = file_path.name
13     # 打印文件名字
14     print(f'文件名为----:{file_name}')
15     suffix = file_path.suffix
16     # 打印文件后缀名
17     print(f'文件后缀名为----:{suffix}')
  •  提取文件夹内所有工作簿的文件名
 1 from pathlib import Path  # 导入pathlib模块的path类
 2 
 3 # Press the green button in the gutter to run the script.
 4 if __name__ == '__main__':
 5     folder_path = Path('D:\\demo\\python\\test_excel\\file_to')
 6     file_list = folder_path.glob('*.xlsx')
 7     print(f'file_list----:{file_list}')
 8     lists = []
 9     for test in file_list:
10         file_name = test.name
11         lists.append(file_name)
12     print(f'lists----:{lists}')

 

posted @ 2022-11-13 18:17  一只野生的小胖鱼  阅读(22)  评论(0编辑  收藏  举报