xlwings: Shapes: Polylines and Polygons

To draw polylines, polygons, and curves with xlwings, there are some issues. Use another Python package called `comtypes`, which is based on COM, similar to xlwings.

First, install the `comtypes` library using the `pip` command in the DOS command window:

pip install comtypes

Then, enter the following in the Python IDLE window:

#Import the CreateObject function from comtypes
from comtypes.client import CreateObject
app2=CreateObject("Excel.Application")    #Create Excel application
app2.Visible=True  #Make the application window visible
bk2=app2.Workbooks.Add()    #Add a workbook
sht2=bk2.Sheets(1)  #Get the first sheet
pts=[[10,10], [50,150],[90,80], [70,30], [10,10]]    #Polygon vertices
sht2.Shapes.AddPolyline(pts)    #Add polygon region

Code:

#Drawing Polyline and Polygon

#Import `CreateObject` function from `comtypes` package
from comtypes.client import CreateObject

app=CreateObject('Excel.Application')    #Create Excel application
app.Visible=True    #Make the application window visible
bk=app.Workbooks.Add()    #Add a workbook
sht=bk.Sheets(1)    #Get the first worksheet

pts=[[10,10], [50,150],[90,80], [70,30], [10,10]]    #Polygon vertices
sht.Shapes.AddPolyline(pts)    #Add polygon area

#bk.save()
#bk.close()
#app.kill()


category : xlwings: Shapes

Leave a Reply

Your email address will not be published. Required fields are marked *

Zoomed Image