帮做一个提取文件名称转换为Excel的代码
下面是一个使用Python编写的将文件名称提取并转换为Excel的代码示例:
import os
import xlwt
def extract_filenames_to_excel(folder_path, excel_file):
# 创建一个Excel文件
workbook = xlwt.Workbook()
sheet = workbook.add_sheet('File Names')
# 设置表头
sheet.write(0, 0, '序号')
sheet.write(0, 1, '文件名')
# 遍历文件夹中的文件
file_count = 1
for root, dirs, files in os.walk(folder_path):
for file in files:
# 提取文件名
filename = os.path.splitext(file)[0]
# 将文件名写入Excel
sheet.write(file_count, 0, file_count)
sheet.write(file_count, 1, filename)
file_count += 1
# 保存Excel文件
workbook.save(excel_file)
# 示例用法
extract_filenames_to_excel('/path/to/folder', 'file_names.xls')
请将/path/to/folder替换为实际的文件夹路径,将file_names.xls替换为要保存的Excel文件名。运行代码后,在指定的文件夹中将生成一个名为file_names.xls的Excel文件,其中包含文件名称的列表
原文地址: https://www.cveoy.top/t/topic/hJKM 著作权归作者所有。请勿转载和采集!