I need to insert a text box in an Excel sheet using python, I have tried openpyxl, openpyxl.drawing.shape more specifically, xlsxwriter and on and it just doesn't work.
from openpyxl import Workbook
from openpyxl.drawing.text import Paragraph, ParagraphProperties
from openpyxl.drawing.text import CharacterProperties
from openpyxl.drawing.shape import Shape
# Create a shape
shape = Shape()
# Initialize GraphicalProperties
graphical_properties = GraphicalProperties()
# Assign GraphicalProperties to spPr
shape.spPr = graphical_properties
# Add the shape to the worksheet
sheet.add_shape(shape)
# Save the workbook
workbook.save("output.xlsx")
This is the latest code that I tried.
Openpyxl doesn't play well with shapes so better to use Xlsxwriter as mentioned or if you want to add to an existing workbook you can use Xlwings or win32com.
The code below is an example for Xlwings;
The AddShape command is from Excel and the shape type is from the MsoAutoShapeType enumeration, e.g. 1 is a rectangle and 9 is a circle.