使用openpyxl编写多个Excel文件

问题描述

您好

我正在学习Python的业余爱好,看它是否可以帮助我的工作。我在一个新项目中,需要编写多个excel文件。目标是列出多个文件夹中的文件。这是我到目前为止构建的代码
import os
import openpyxl
from openpyxl import load_workbook

wb = openpyxl.Workbook()
    
#load an excel file which includes directory lists
load_drt = load_workbook("D:/picture/directory_list.xlsx",data_only=True)
load_ws = load_drt["Sheet"]

col = 0
    
# loading each cell of the directory file
for n in range(5) :
    try :
        col = 1 + col
        sheet = load_drt.active
        cellvalue = sheet.cell(row=1,column=col).value
        name = "{}".format(cellvalue)

        # reading file list of each folder and write the list to an excel named after the folder name
        for i in range(10):
            try:
                z = str(i+1).zfill(2)
                path = "D:/picture/{}/{}".format(name,z)
                file_list = os.listdir(path)
            except:
                print("no data")
            else:
                print(file_list)
                sheet = wb.active
                sheet.append(file_list)
        
        wb.save("D:/picture/{}.xlsx".format(name))

    except:
        print("no data")

仅供参考,“ directory_list.xlsx”是一个文件,其中包含“ D:/ picure /”中的文件名称

您也许能够猜出我对代码的期望。我所期望的类似于以下内容

例如,我有两个文件夹,名称分别为“ A”和“ B”

,其中有图片文件。“ A”的文件名为'01 -1.jpg ,01-2.jpg,03-1.jpg,04-1.jpg,04-2.jpg'和“ B”具有'01 -1.jpg,01-3.jpg, 02-1.jpg'我从代码中期望的结果如下所示。

enter image description here

enter image description here

这些是我期望的excel文件“ A.xlsx”和excel“文件B.xlsx”的值。即使“ A.xlsx”符合我的期望,“ B.xlsx”也与我的期望不同“ B.xlsx”包括这样的“ A xlsx”的值。

enter image description here

我含糊地理解出了什么问题并试图修复它,但是失败了。请帮助。谢谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)