使用openpyxl遇TypeError: string indices must be integers问题求助及资源推荐
TypeError: string indices must be integers in openpyxl What's Likely Going Wrong
First, let's break down why this error pops up: it means you're trying to access a string using something that's not an integer (like a string key, e.g., my_string['value']). Since you mentioned the cell has the number 55 and adding .value didn't fix it, here are the most common scenarios to check:
Scenario 1: You're treating a cell coordinate string like a cell object
If you wrote code like this (mistaking a coordinate string for an actual cell object):
cell = 'A1' # This is just a string, not a cell! print(cell['value']) # Boom—TypeError hits here
Fix: Grab the real cell object from the sheet first, then use .value:
from openpyxl import load_workbook wb = load_workbook('your_file.xlsx') sheet = wb.active cell = sheet['A1'] # Now this is a proper cell object print(cell.value) # Should return 55 as an integer
Scenario 2: You accidentally converted the cell object to a string
Sometimes when debugging, you might turn the cell into a string (e.g., str(cell)), which gives you a representation like <Cell 'Sheet1'.A1>—not the actual data. Trying to index that string with a key like 'value' will fail:
cell = sheet['A1'] str_cell = str(cell) print(str_cell['value']) # Error! Strings only accept integer indices
Fix: Skip converting to string, directly use .value on the original cell object.
Scenario 3: You're using string indices on a row/tuple instead of a cell
If you're iterating over rows with iter_rows() or iter_cols(), each row is a tuple of cell objects (or raw values if you use values_only=True). Trying to index that tuple with a string will throw the error:
for row in sheet.iter_rows(values_only=True): print(row['value']) # Row is a tuple like (55,), not a dictionary
Fix: Use integer indices to access items in the tuple:
for row in sheet.iter_rows(values_only=True): print(row[0]) # Gets the first cell's value (55 in your case)
Quick Debug Tip
Add print(type(your_variable)) right before the line that throws the error. This will tell you if you're working with a string, cell object, tuple, or something else—super helpful for narrowing down the issue!
I totally get your frustration with openpyxl's documentation—it can feel scattered for beginners. Here are some solid, beginner-friendly resources to level up:
- Automate the Boring Stuff with Python: This book has a fantastic chapter on Excel automation using openpyxl (and other libraries). The examples are practical, step-by-step, and focused on solving real-world problems like updating spreadsheets or extracting data. It’s perfect for someone new to Python.
- Real Python's Excel Series: Their articles dive deep into openpyxl, covering everything from basic reads/writes to advanced features like styling and formula handling. Explanations are clear, and every section includes runnable code snippets you can tweak for your own projects.
- Openpyxl's Official Examples: Even if the full docs feel overwhelming, the official "Usage Examples" section has bite-sized code samples for common tasks. It’s a great place to copy-paste code and adapt it to your needs.
- Hands-On Video Tutorials: Free tutorials on platforms like YouTube focused specifically on openpyxl are a win for visual learners. Look for videos that walk through building small projects (like a budget tracker or data extractor)—hands-on practice beats reading docs alone.
- Stack Overflow & r/learnpython: When you hit a snag, search Stack Overflow for your error message—chances are someone’s already solved it. For broader questions, the r/learnpython subreddit is full of friendly folks who love guiding new programmers.
内容的提问来源于stack exchange,提问作者user3672549

