Python-从Excel列列表创建多个文件夹 [英] Python - Create multiple folders from an excel column list
本文介绍了Python-从Excel列列表创建多个文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我基本上是想让Python在目录中创建一堆文件夹,每个文件夹的名称基于Excel文件中的列表.该列表在D列中,标题为文件夹名称".
I am basically trying to get Python to create a bunch of folders in a directory with each folders name based on a list in an Excel file. The list is in column D, that has a heading "Folder Name".
我已经能够在单个单元中执行此操作,但是努力弄清楚如何多次执行该操作.我到目前为止的代码如下.
I have been able to do this with an individual cell, but struggling to figure out how to do it for multiple. The code I have so far is below.
非常感谢您的帮助-我对此很陌生!`
Your help is really appreciated - I am very new to this!`
import os
import openpyxl
def folder_creation(EXCEL_FILE_DIRECTORY, FOLDER_CREATION_LOCATION, EXCEL_FILE_NAME):
os.chdir (EXCEL_FILE_DIRECTORY)
workbook = openpyxl.load_workbook (EXCEL_FILE_NAME)
sheet = workbook.get_sheet_by_name ('Sheet1')
folderName = sheet ['D2'].value
baseDir = FOLDER_CREATION_LOCATION
os.makedirs(os.path.join(baseDir, folderName))
print ("\nFolder created in: ", os.path.join(baseDir, folderName))
推荐答案
您必须遍历所有列值.这对我有用(openpyxl v2.5):
You have to iterate through all column values. This works for me (openpyxl v2.5):
def folder_creation(EXCEL_FILE_DIRECTORY, FOLDER_CREATION_LOCATION, EXCEL_FILE_NAME):
os.chdir(EXCEL_FILE_DIRECTORY)
workbook = openpyxl.load_workbook(EXCEL_FILE_NAME)
sheet = workbook.get_sheet_by_name('Sheet1')
col_values = [cell.value for col in sheet.iter_cols(
min_row=2, max_row=None, min_col=4, max_col=4) for cell in col]
for value in col_values:
folderName = value
baseDir = FOLDER_CREATION_LOCATION
os.makedirs(os.path.join(baseDir, folderName))
print("\nFolder created in: ", os.path.join(baseDir, folderName))
这篇关于Python-从Excel列列表创建多个文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文