1

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.

1
  • 1
    Welcome to SO. Fwiw, workbook is a string since you've set it in workbook = ("Data.xlsx"). you're not using xlswriter at all. Did you intend to do workbook = xlswriter.Workbook("Data.xlsx")? And you also don't need to put workbook = ... inside the loop. Commented Oct 6, 2020 at 0:08

1 Answer 1

1

At no point in your code are you utilizing xlsxwriter. You simply iterate over a directory and try to use a add_worksheet method on a string, as the traceback shows. Just wrapping "Data.xlsx" in a tuple does not magically make it an Excel workbook. You will need to check the documentation for how to use xlsxwriter and how to open files with it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.