Python Forum
Error "list indices must be integers or slices, not str"
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error "list indices must be integers or slices, not str"
#1
I want to format Excel column to percent. The following code gave me "list indices must be integers or slices, not str" error. The __class__ of Column D is 'str'. Is it because the header is string?

Please advise how to format the columns with %. Thank you.

    wb = xl.load_workbook(fullpath)
    wsData = wb.worksheets["Data"]    
    wsData.Range("D2:O2").Style = "Percent"
or
    wsData.Range("D2:O2").NumberFormat = "0.00%"
See attachment for data.
   
Yoriz write Dec-30-2022, 12:34 AM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
I'm assuming you are using openpyxl. I may be wrong since only 1 line in your post is valid based on "xl" being openpyxl.

The error is here:
wsData = wb.worksheets["Data"]
Worksheets is not a dictionary. It cannot be indexed by name. You can do this.
wsData = wb["Data"]
After you change that you'll soon learn that worksheets don't have a method "Range()". This is how you can set the number format for a range of cells.
import openpyxl as xl

wb = xl.load_workbook("test.xlsx")
sheet = wb["Sheet1"]
for cell in sheet["D2:O2"][0]:
    cell.number_format = "0.00%"
Have you looked through any of the openpyxl documentation?

https://openpyxl.readthedocs.io/en/stable/tutorial.html
dee likes this post
Reply
#3
Thank you deanhystad. It works.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  tuple indices must be integers or slices, not str cybertooth 16 11,615 Nov-02-2023, 01:20 PM
Last Post: brewer32
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,183 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  boto3 - Error - TypeError: string indices must be integers kpatil 7 1,275 Jun-09-2023, 06:56 PM
Last Post: kpatil
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 1,355 May-22-2023, 10:39 PM
Last Post: ICanIBB
  Response.json list indices must be integers or slices, not str [SOLVED] AlphaInc 4 6,432 Mar-24-2023, 08:34 AM
Last Post: fullytotal
Question How to append integers from file to list? Milan 8 1,457 Mar-11-2023, 10:59 PM
Last Post: DeaD_EyE
  "TypeError: string indices must be integers, not 'str'" while not using any indices bul1t 2 2,047 Feb-11-2023, 07:03 PM
Last Post: deanhystad
  TypeError: string indices must be integers JonWayn 12 3,412 Aug-31-2022, 03:29 PM
Last Post: deanhystad
  read a text file, find all integers, append to list oldtrafford 12 3,618 Aug-11-2022, 08:23 AM
Last Post: Pedroski55
  TypeError: list indices must be integers or slices, not range Anldra12 2 2,591 Apr-22-2022, 10:56 AM
Last Post: Anldra12

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020