需要将目录列表放在 excel 上的每个单元格中
Need to place the directory list onto each cell on excel
我一直在尝试获取目录名称列表并将其放入每个单元格中。我正在尽最大努力学习这个过程中的嵌套循环。每次我尝试它时,只需将最后一个目录名称放在每个单元格中。我一直在尝试多种变体以查看是否有任何修复,但我得到了相同的输出。
from openpyxl import Workbook, load_workbook
from openpyxl.utils import get_column_letter
import os
path = 'C:\Users\jstju\OneDrive\Documents\Test\_test_list.xlsx'
wb = Workbook()
ws = wb.active
path2 = "C:\Users\jstju\OneDrive\Documents\Test\Script"
def columns():
column_A = get_column_letter(1)
column_B = get_column_letter(2)
column_C = get_column_letter(3)
column_D = get_column_letter(4)
column_E = get_column_letter(5)
cols2 = 15
os.chdir(path2)
dir_list = os.listdir(path2)
for col in range(2, 16):
cols_A = column_A + str(col)
ws[cols_A] = col
cols_B = column_B + str(col)
ws[cols_B] = cols2 + col
cols_C = column_C + str(col)
ws[cols_C] = f"=CONCATENATE(A{col},B{col})"
cols_D = column_D + str(col)
ws[cols_D] = '"'
for i in dir_list:
char = ""
col_E = column_E + str(col)
ws[col_E] = char + i
columns()
wb.save(path)
据我了解,您想在目录中获取一个名称列表,然后将一个名称放入新创建的工作簿中 Excel 工作表的列中的每个单元格中。
你可以这样做:
from openpyxl import Workbook
import os
wb = Workbook()
ws = wb.active
path2 = "./Sampledir"
cwd = os.getcwd()
os.chdir(path2)
dir_list = os.listdir('.')
for x in dir_list:
ws.append([x])
os.chdir(cwd)
path = './_test_list.xlsx'
wb.save(path)
在我的测试用例中,./Sampledir
看起来像这样:
C:\python>dir .\Sampledir
Volume in drive C is OS
Volume Serial Number is 4EDD-194A
Directory of C:\python\Sampledir
05/28/2022 05:26 PM <DIR> .
05/28/2022 05:26 PM <DIR> ..
05/28/2022 05:25 PM 0 sample1.txt
05/28/2022 05:25 PM 0 sample2.txt
2 File(s) 0 bytes
2 Dir(s) 243,106,267,136 bytes free
C:\python>
输出电子表格如下所示:
更新:
要添加从任意行和列开始的目录内容,您可以将上面的 for 循环替换为:
topRow, leftColumn = 2, 3
for i, x in enumerate(dir_list):
ws.cell(row=i + topRow, column=leftColumn, value=x)
我一直在尝试获取目录名称列表并将其放入每个单元格中。我正在尽最大努力学习这个过程中的嵌套循环。每次我尝试它时,只需将最后一个目录名称放在每个单元格中。我一直在尝试多种变体以查看是否有任何修复,但我得到了相同的输出。
from openpyxl import Workbook, load_workbook
from openpyxl.utils import get_column_letter
import os
path = 'C:\Users\jstju\OneDrive\Documents\Test\_test_list.xlsx'
wb = Workbook()
ws = wb.active
path2 = "C:\Users\jstju\OneDrive\Documents\Test\Script"
def columns():
column_A = get_column_letter(1)
column_B = get_column_letter(2)
column_C = get_column_letter(3)
column_D = get_column_letter(4)
column_E = get_column_letter(5)
cols2 = 15
os.chdir(path2)
dir_list = os.listdir(path2)
for col in range(2, 16):
cols_A = column_A + str(col)
ws[cols_A] = col
cols_B = column_B + str(col)
ws[cols_B] = cols2 + col
cols_C = column_C + str(col)
ws[cols_C] = f"=CONCATENATE(A{col},B{col})"
cols_D = column_D + str(col)
ws[cols_D] = '"'
for i in dir_list:
char = ""
col_E = column_E + str(col)
ws[col_E] = char + i
columns()
wb.save(path)
据我了解,您想在目录中获取一个名称列表,然后将一个名称放入新创建的工作簿中 Excel 工作表的列中的每个单元格中。
你可以这样做:
from openpyxl import Workbook
import os
wb = Workbook()
ws = wb.active
path2 = "./Sampledir"
cwd = os.getcwd()
os.chdir(path2)
dir_list = os.listdir('.')
for x in dir_list:
ws.append([x])
os.chdir(cwd)
path = './_test_list.xlsx'
wb.save(path)
在我的测试用例中,./Sampledir
看起来像这样:
C:\python>dir .\Sampledir
Volume in drive C is OS
Volume Serial Number is 4EDD-194A
Directory of C:\python\Sampledir
05/28/2022 05:26 PM <DIR> .
05/28/2022 05:26 PM <DIR> ..
05/28/2022 05:25 PM 0 sample1.txt
05/28/2022 05:25 PM 0 sample2.txt
2 File(s) 0 bytes
2 Dir(s) 243,106,267,136 bytes free
C:\python>
输出电子表格如下所示:
更新:
要添加从任意行和列开始的目录内容,您可以将上面的 for 循环替换为:
topRow, leftColumn = 2, 3
for i, x in enumerate(dir_list):
ws.cell(row=i + topRow, column=leftColumn, value=x)