vee
vee

Reputation: 119

How to Update list in excel file with openpyxl?

I have template excel file.I need to update files name list in directory into Excel file. Like this: enter image description here

This's my code:

import xlsxwriter 
import os
import datetime
import openpyxl
import pathlib
import xlsxwriter
arr = os.listdir('C:/Users/11359023/Desktop/source')
xfile = openpyxl.load_workbook('C:/Users/11359023/Desktop/vee_report.xlsx')
sheet = xfile.get_sheet_by_name('Property Files Report')
x=0
col = "A"
row = x
for f in arr:
    sheet.write(row,col,f)
    row += 1
xfile.save('vee_report.xlsx')

When i run this code.It not update to my excel file and get error "Worksheet' object has no attribute 'write".Please tell me how to fix this problem.

Upvotes: 0

Views: 129

Answers (1)

Metalgear
Metalgear

Reputation: 3457

You should set value of the cell in work sheet as the following.

x = 1 # not x = 0
...
sheet['{0}{1}'.format(col, row)].value = f

Hope it could help you.

Upvotes: 1

Related Questions