Blog
How To Create Rectangles, Rounded Rectangles, Ellipses, and Circles Using xlwings?
Method
Use the `AddShape` method of the Shapes object to create rectangles, rounded rectangles, ellipses, and circles.
|
Name |
Value |
Description |
|
msoShapeRectangle |
1 |
Rectangle |
|
msoShapeRoundedRectangle |
5 |
Rounded Rectangle |
|
msoShapeOval |
9 |
Ellipse |
sht.api.Shapes.AddShape(1, 50, 50, 100, 200) #Draw a rectangle face
sht.api.Shapes.AddShape(5, 100, 100, 100, 200) #Draw a rounded rectangle face
sht.api.Shapes.AddShape(9, 150, 150, 100, 200) #Draw an ellipse face
sht.api.Shapes.AddShape(9, 200, 200, 100, 100) #Draw a circle face
shp1=sht.api.Shapes.AddShape(1, 250, 50, 100, 200) #Draw a rectangle
shp2=sht.api.Shapes.AddShape(5, 300, 100, 100, 200) #Draw a rounded rectangle
shp3=sht.api.Shapes.AddShape(9, 350, 150, 100, 200) #Draw an ellipse
shp4=sht.api.Shapes.AddShape(9, 400, 200, 100, 100) #Draw a circle
shp1.Fill.Visible=False
shp2.Fill.Visible=False
shp3.Fill.Visible=False
shp4.Fill.Visible=False
Sample Code
#Copy and paste formulas
import xlwings as xw #Import the xlwings package
from xlwings import constants as con
import os #Import the os package
root = os.getcwd() #Get the current path
#Create an Excel application window, visible,
#without opening a workbook
app=xw.App(visible=True, add_book=False)
#Open a data file, writable
bk=app.books.open(fullname=root+r'\Formula2.xlsx',read_only=False)
sht=bk.api.Sheets(1) #Get the worksheet
#Copy and paste formulas
sht.Range('B3:E5').Copy()
sht.Range('D7:G9').PasteSpecial(Paste=con.PasteType.xlPasteFormulas)
#bk.save()
#bk.close()
#app.kill()

How To Create Line Segments Using xlwings?
Method
Use the `AddLine` method of the Shapes object to create a line segment. The method syntax is:
sht.api.Shapes.AddLine(BeginX, BeginY, EndX, EndY)
Where `sht` refers to a worksheet object. The parameters `BeginX, BeginY` represent the coordinates of the starting point, and `EndX, EndY` represent the coordinates of the endpoint. This method returns a Shape object representing the line segment.
shp=sht.api.Shapes.AddLine(10,10,250,250) #Create a line segment `Shape` object
ln=shp.Line #Get the line shape object
#Set properties of the line shape object: line style, color, and width
ln.DashStyle=3
ln.ForeColor.RGB=xw.utils.rgb_to_int((255, 0, 0))
ln.Weight=5
Sample Code
#Check if a specified cell range contains a formula
import xlwings as xw #Import the xlwings package
import os #Import the os package
root = os.getcwd() #Get the current path
#Create an Excel application window, visible,
#without opening a workbook
app=xw.App(visible=True, add_book=False)
#Open a data file, writable
bk=app.books.open(fullname=root+r'\Formula2.xlsx',read_only=False)
sht=bk.api.Sheets(1) #Get the worksheet
#Check if a specified cell range contains a formula
rng=sht.Range('B3:E5')
for cell in rng:
if cell.HasFormula==True:
print(cell.Row,cell.Column,'Yes')
#bk.save()
#bk.close()
#app.kill()

How To Create Points Using xlwings?
Method
The Shapes object does not provide a specific method to draw points, but it offers several special shapes that can represent points, such as stars, rectangles, circles, diamonds, etc. These AutoShapes can be created using the `AddShape` method of the Shapes object. The method syntax is:
sht.api.Shapes.AddShape(Type, Left, Top, Width, Height)
Using the xlwings package API call. `sht` refers to a worksheet object. This method returns a Shape object. The parameters are as follows:
|
Name |
Required/Optional |
Data Type |
Description |
|
Type |
Required |
MsoAutoShapeType |
The type of AutoShape to create |
|
Left |
Required |
Single |
Position of the shape’s top-left corner relative to the document’s top-left corner (in points) |
|
Top |
Required |
Single |
Position of the shape’s top-left corner relative to the document’s top (in points) |
|
Width |
Required |
Single |
Width of the shape’s border (in points) |
|
Height |
Required |
Single |
Height of the shape’s border (in points) |
|
Name |
Value |
Description |
|
msoShape10pointStar |
149 |
10-point star |
|
msoShape12pointStar |
150 |
12-point star |
|
msoShape16pointStar |
94 |
16-point star |
|
msoShape24pointStar |
95 |
24-point star |
|
msoShape32pointStar |
96 |
32-point star |
|
msoShape4pointStar |
91 |
4-point star |
|
msoShape5pointStar |
92 |
5-point star |
|
msoShape6pointStar |
147 |
6-point star |
sht.api.Shapes.AddShape(92,180,80,10,10)
sht.api.Shapes.AddShape(150,150,40,15,15)
sht.api.Shapes.AddShape(96,80,80,3,3)
Sample Code
#Find cells containing formulas
import xlwings as xw #Import the xlwings package
from xlwings import constants as con
import os #Import the os package
root = os.getcwd() #Get the current path
#Create an Excel application window, visible,
#without opening a workbook
app=xw.App(visible=True, add_book=False)
#Open a data file, writable
bk=app.books.open(fullname=root+r'\Formula2.xlsx',read_only=False)
sht=bk.api.Sheets(1) #Get the worksheet
#Find cells containing formulas
rng=sht.Range('B3:E5').SpecialCells(con.CellType.xlCellTypeFormulas)
rng.Select()
#bk.save()
#bk.close()
#app.kill()

How To Use Evaluate Method Using xlwings?
Method
sht.Range(‘G7’).Value=app.api.Evaluate(‘=AVERAGE($A$1:$A$5)’)
Sample Code
# Use `Evaluate` method
import xlwings as xw #Import the xlwings package
import os #Import the os package
root = os.getcwd() #Get the current path
#Create an Excel application window, visible,
#without opening a workbook
app=xw.App(visible=True, add_book=False)
#Open a data file, writable
bk=app.books.open(fullname=root+r'\Formula.xlsx',read_only=False)
sht=bk.api.Sheets(1) #Get the worksheet
#Use `Evaluate` method
sht.Range('G7').Value=app.api.Evaluate('=AVERAGE($A$1:$A$5)')
bk.save('test.xlsx')
#bk.close()
#app.kill()

How To Use WorksheetFunction Property Using xlwings?
Method
sht.Range(‘G8’).Value=app.api.WorksheetFunction.Average(sht.Range(‘A1:A5’))
Sample Code
# Use the `WorksheetFunction` property
import xlwings as xw #Import the xlwings package
import os #Import the os package
root = os.getcwd() #Get the current path
#Create an Excel application window, visible,
#without opening a workbook
app=xw.App(visible=True, add_book=False)
#Open a data file, writable
bk=app.books.open(fullname=root+r'\Formula.xlsx',read_only=False)
sht=bk.api.Sheets(1) #Get the worksheet
#Use the `WorksheetFunction` property
sht.Range('G8').Value=app.api.WorksheetFunction.Average(sht.Range('A1:A5'))
bk.save('test.xlsx')
#bk.close()
#app.kill()

How To Enter Array Formulas using FormulaArray Property Using xlwings?
Method
sht.Range(‘E7′).FormulaArray=’=SUM(A1:A5*B1:B5)’
Sample Code
# Use `FormulaArray` property for array formulas
import xlwings as xw #Import the xlwings package
import os #Import the os package
root = os.getcwd() #Get the current path
#Create an Excel application window, visible,
#without opening a workbook
app=xw.App(visible=True, add_book=False)
#Open a data file, writable
bk=app.books.open(fullname=root+r'\Formula.xlsx',read_only=False)
sht=bk.api.Sheets(1) #Get the worksheet
#Use `FormulaArray` property for array formulas
sht.Range('E7').FormulaArray='=SUM(A1:A5*B1:B5)'
bk.save('test.xlsx')
#bk.close()
#app.kill()

How To Enter R1C1 Style Formulas using FormulaR1C1 Property Using xlwings?
Method
sht.Range(‘C8′).FormulaR1C1=’=SQRT(R1C1)’
Sample Code
# Use `FormulaR1C1` property for R1C1-style formulas
import xlwings as xw #Import the xlwings package
import os #Import the os package
root = os.getcwd() #Get the current path
#Create an Excel application window, visible,
#without opening a workbook
app=xw.App(visible=True, add_book=False)
#Open a data file, writable
bk=app.books.open(fullname=root+r'\Formula.xlsx',read_only=False)
sht=bk.api.Sheets(1) #Get the worksheet
#Use `FormulaR1C1` property for R1C1-style formulas
sht.Range('C8').FormulaR1C1='=SQRT(R1C1)'
bk.save('test.xlsx')
#bk.close()
#app.kill()

How To Enter A1 Style Formulas using Formula Property Using xlwings?
Method
sht.Range(‘C7′).Formula=’=$A$1+$A$5’
Sample Code
# Use `Formula` property for A1-style formulas
import xlwings as xw #Import the xlwings package
import os #Import the os package
root = os.getcwd() #Get the current path
#Create an Excel application window, visible,
#without opening a workbook
app=xw.App(visible=True, add_book=False)
#Open a data file, writable
bk=app.books.open(fullname=root+r'\Formula.xlsx',read_only=False)
sht=bk.api.Sheets(1) #Get the worksheet
#Use `Formula` property for A1-style formulas
sht.Range('C7').Formula='=$A$1+$A$5'
bk.save('test.xlsx')
#bk.close()
#app.kill()

How To Enter Regular Formulas Using xlwings?
Method
sht.Range(‘A7′).Value=’=A1*B2’
sht.Range(‘A8′).Value=’=SUM(A1:A5)’
Sample Code
#Adding Formulas - Regular Formulas
import xlwings as xw #Import the xlwings package
import os #Import the os package
root = os.getcwd() #Get the current path
#Create an Excel application window, visible,
#without opening a workbook
app=xw.App(visible=True, add_book=False)
#Open a data file, writable
bk=app.books.open(fullname=root+r'\Formula.xlsx',read_only=False)
sht=bk.api.Sheets(1) #Get the worksheet
#Enter regular formulas
sht.Range('A7').Value='=A1*B2'
sht.Range('A8').Value='=SUM(A1:A5)'
bk.save('test.xlsx')
#bk.close()
#app.kill()

How To Exchange Excel Worksheet and Python Dictionary Data Using xlwings?
Method
Reading
#xlwings
sht.range(‘A1:B2’).options(dict).value
#sht.range(‘A4:B5’).options(dict, transpose=True).value
Writing
#xlwings
#dic={ ‘a’: 1.0, ‘b’: 2.0}
#sht.range(‘A1:B2’).options(dict).value=dic
#sht.range(‘A4:B5’).options(dict, transpose=True).value=dic
Sample Code
#Data Read/Write Between Excel Sheets and Python Dictionaries
import xlwings as xw #Import the xlwings package
app=xw.App()
bk=app.books.open('data.xlsx')
sht=bk.sheets.active #Get the active worksheet
#Reading
#xlwings
#print(sht.range('A1:B2').options(dict).value)
#print(sht.range('A4:B5').options(dict, transpose=True).value)
#Writing
#xlwings
dic={ 'a': 1.0, 'b': 2.0}
sht.range('A1:B2').options(dict).value=dic
sht.range('A4:B5').options(dict, transpose=True).value=dic
#bk.close()
#app.kill()


