fcr
fcr

Reputation: 165

Write two dimension list in excel with openpyxl

I've been trying to come up with a way to write two-dimensinal list in python using openpyxl, I got a list of pdfs in a folder, each one with different values separated by an underline ("year_day_month_name.pdf") in the file name, so far I've been able to extract the values, put them into a list and write the title for each column in excel, but I cant find a way to extract the value from the list and iterate column and rows to write them in the excel file

I've only been able to write the first value of the list with the last part of the code, but I dont think that's the way, thanks

from os import listdir
from openpyxl import Workbook

root = "C:\z_PruebPy"

multilist = []


for files in listdir(root):
    multilist.append(str(files).strip(".pdf"))

ldel = []

for i in multilist:
     ldel.append(i.split("_"))

print(ldel)

for pp in ldel:
    print(pp)

book = Workbook()
sheet = book.active

columns = ["YEAR", "MONTH", "DAY", "NUMBER", "TIPEB", "ROL", "ST", "NUM",
      "USE", "PROP"]

row = 1
for i, value in enumerate(columns):
    sheet.cell(column=i+1, row=row, value=value)

for pe in ldel:
    sheet.cell(column=1, row=row+1, value=pe[0])
    sheet.cell(column=2, row=row + 1, value=pe[1])
    sheet.cell(column=3, row=row + 1, value=pe[2])


book.save("C:/z_PruebPy/output.xlsx")

Upvotes: 1

Views: 1339

Answers (1)

Charlie Clark
Charlie Clark

Reputation: 19507

I don't really understand quite what you're tying to do but your code is unnecessarily complex.

ws.append(columns)
for pe in ldel:
    ws.append(pe)

Ought to work.

Upvotes: 3

Related Questions