如何为.xls工作表python中的每个项目创建目录

如何为.xls工作表python中的每个项目创建目录,python,Python,我需要帮助,我正在尝试为电子表格中的每个项目(行)创建一个目录,如何使用python 3.5实现这一点。 我尝试使用pip和conda安装pandas,但它不起作用,错误是说我需要visual c++构建工具。即使在我安装了工具之后,也会发生相同的错误。 panda是为.xls工作表中的每一行创建目录的最佳方法吗?我有多个.xls文件如果将文件另存为CSV,则此任务将更轻松。试试这个: import csv, sys, os folder_list = [] with open('folder

我需要帮助,我正在尝试为电子表格中的每个项目(行)创建一个目录,如何使用python 3.5实现这一点。 我尝试使用pip和conda安装pandas,但它不起作用,错误是说我需要visual c++构建工具。即使在我安装了工具之后,也会发生相同的错误。
panda是为.xls工作表中的每一行创建目录的最佳方法吗?我有多个.xls文件

如果将文件另存为CSV,则此任务将更轻松。试试这个:

import csv, sys, os

folder_list = []
with open('folders.csv', 'rb') as f:
    reader = csv.reader(f)
    for row in reader:
        for item in row:
            if item != None:
                folder_list.append(item)
                print item

for folder in folder_list:
    try:
        os.makedirs(folder)
    except WindowsError as e:
        pass

我对你的问题有些不太清楚,但我会尽力为你提供一些想法。 您可以使用xlrd(这里的文档:)

让我们假设您有一个文件“sample.xls”,其中包含许多工作表。对于每个工作表,您希望在“C:\test”(假设它是Windows路径)中创建与该工作表中的行数相同的文件夹。我们还假设您希望使用工作表的名称,后跟一个递增的数字 命名这些文件夹(您可以轻松编辑代码以满足实际需要)

PS:我同意使用CSV文件会让事情变得更简单

编辑:

以下解决方案基于这样一种假设:您希望创建的文件夹数量与xls文件中每个工作表中的(非空)单元格数量相同,并且每个文件夹的名称格式为“sheetName_rowi_colj” 其中i和j是两个指数,它们与表格中的单元格位置有关

import os
from xlrd import open_workbook

parentPath = r"C:\test"
xlsFile = open_workbook('sample.xls',ragged_rows=True) # Open the xls file
for sheetName in xlsFile.sheet_names(): # Loop over the sheets inside the xls file
    for rowIdx in range(xlsFile.sheet_by_name(sheetName).nrows): # Loop over the rows
        for colIdx in range(xlsFile.sheet_by_name(sheetName).row_len(rowIdx)): # Loop over the columns for each row
            if xlsFile.sheet_by_name(sheetName).cell_value(rowIdx,colIdx) != '': # Check if the cell is empty
                childPath = ''.join([sheetName, '_row', str(rowIdx+1), '_col', str(colIdx+1)]) # +1 because indices start from zero
                newPath = os.path.join(parentPath,childPath) 
                if not os.path.exists(newPath): # Make sure the path does not exist
                    os.makedirs(newPath)

如果您有许多xls文件,只需在其上循环。

您是否尝试过使用内置VBA?
import os
from xlrd import open_workbook

parentPath = r"C:\test"
xlsFile = open_workbook('sample.xls',ragged_rows=True) # Open the xls file
for sheetName in xlsFile.sheet_names(): # Loop over the sheets inside the xls file
    for rowIdx in range(xlsFile.sheet_by_name(sheetName).nrows): # Loop over the rows
        for colIdx in range(xlsFile.sheet_by_name(sheetName).row_len(rowIdx)): # Loop over the columns for each row
            if xlsFile.sheet_by_name(sheetName).cell_value(rowIdx,colIdx) != '': # Check if the cell is empty
                childPath = ''.join([sheetName, '_row', str(rowIdx+1), '_col', str(colIdx+1)]) # +1 because indices start from zero
                newPath = os.path.join(parentPath,childPath) 
                if not os.path.exists(newPath): # Make sure the path does not exist
                    os.makedirs(newPath)