I'm trying to write a python script where I add multiple worksheets to an excel document and each sheet's name is the name of a folder and I keep getting the error "AttributeError: 'str' object has no attribute 'add_worksheet'"
Here is a sample of my code:
import os
import xlsxwriter
directory = r"D:\Work\Folders"
workbook = (r"D:\Work\Folders\Data.xlsx")
for folder in os.listdir(directory):
workbook = ("Data.xlsx")
workbook.add_worksheet(folder)
print (folder)
Its simple but I cant seam to figure out what the issue is.
workbookis a string since you've set it inworkbook = ("Data.xlsx"). you're not using xlswriter at all. Did you intend to doworkbook = xlswriter.Workbook("Data.xlsx")? And you also don't need to putworkbook = ...inside the loop.