如何使用OpenPyxl插入文本形状及在图表中添加文本框?
Hey there! Let's break down your questions one by one and fix that frustrating error you ran into.
To add a shape that holds text (like a text box or labeled rectangle), you'll work with openpyxl.drawing.shapes to create the shape, configure its text content, then add it directly to your worksheet's shape collection. Here's a concrete example:
from openpyxl import Workbook from openpyxl.drawing.shapes import Shape, TextFrame from openpyxl.drawing.text import Paragraph, ParagraphProperties, CharacterProperties wb = Workbook() ws = wb.active # Create a rectangular shape (you can use "textBox" for a dedicated text box too) text_shape = Shape( shape_type="rect", width=250000, # Units are EMUs; ~1cm = 360000 EMUs height=120000, left=150000, # Position from worksheet's left edge top=150000 # Position from worksheet's top edge ) # Configure text content and styling text_frame = TextFrame() paragraph = Paragraph() paragraph.rich_text.append("This is my text shape!") # Optional: Add styling (center-align, bold 14pt text) paragraph.pPr = ParagraphProperties(algn="ctr") paragraph.endParaRPr = CharacterProperties(sz=14, b=True) text_frame.paragraphs.append(paragraph) text_shape.text = text_frame # Add the shape to the worksheet ws.shapes.add(text_shape) wb.save("text_shape_demo.xlsx")
First, let's fix that AttributeError:
- You likely imported the wrong module (singular
openpyxl.drawing.shapeinstead of pluralopenpyxl.drawing.shapes) add_shapeisn't a module method — you need to useworksheet.shapes.add()orchart.drawing.shapes.add()instead
Here are two approaches for adding text to/around a chart:
Option 1: Text Box Above the Chart (Worksheet Level)
This places a text box on the worksheet, aligned to sit above your chart (looks like it's part of the chart area):
from openpyxl import Workbook from openpyxl.chart import BarChart, Reference from openpyxl.drawing.shapes import Shape, TextFrame from openpyxl.drawing.text import Paragraph wb = Workbook() ws = wb.active # Add sample chart data for i in range(1, 6): ws.append([f"Product {i}", i * 15]) # Create and position the chart chart = BarChart() data = Reference(ws, min_col=2, min_row=1, max_row=5) categories = Reference(ws, min_col=1, min_row=1, max_row=5) chart.add_data(data, titles_from_data=True) chart.set_categories(categories) chart.title = "Monthly Sales" chart.left = 200000 chart.top = 100000 ws.add_chart(chart, "A7") # Create text box aligned above the chart header_text = Shape( shape_type="textBox", width=chart.width, # Match chart's width for alignment height=60000, left=chart.left, # Align with chart's left edge top=chart.top - 70000 # Position above the chart title ) # Add text content text_frame = TextFrame() paragraph = Paragraph() paragraph.rich_text.append("Q3 Sales Performance") text_frame.paragraphs.append(paragraph) header_text.text = text_frame # Add to worksheet shapes ws.shapes.add(header_text) wb.save("chart_text_above.xlsx")
Option 2: Embedded Text Box Inside the Chart
To place text directly inside the chart's area, you'll add the shape to the chart's drawing object instead of the worksheet:
from openpyxl import Workbook from openpyxl.chart import BarChart, Reference from openpyxl.drawing.shapes import Shape from openpyxl.drawing.text import TextFrame, Paragraph wb = Workbook() ws = wb.active # Sample chart data for i in range(1, 6): ws.append([f"Region {i}", i * 20]) # Create chart chart = BarChart() data = Reference(ws, min_col=2, min_row=1, max_row=5) categories = Reference(ws, min_col=1, min_row=1, max_row=5) chart.add_data(data, titles_from_data=True) chart.set_categories(categories) chart.title = "Regional Revenue" ws.add_chart(chart, "A7") # Create embedded text box chart_label = Shape( shape_type="textBox", width=180000, height=70000, left=60000, # Position relative to the chart's top-left corner top=80000 ) # Add text content text_frame = TextFrame() paragraph = Paragraph() paragraph.rich_text.append("Top-performing region!") text_frame.paragraphs.append(paragraph) chart_label.text = text_frame # Add the shape to the chart's drawing chart.drawing.shapes.add(chart_label) wb.save("chart_embedded_text.xlsx")
Pro tip: Make sure you're using the latest version of openpyxl (pip install --upgrade openpyxl) — older versions have limited support for chart-embedded shapes.
内容的提问来源于stack exchange,提问作者Mattman85208




