The InchesToPoints member of the Application object in Excel is a method used to convert a measurement from inches to points. In the context of Excel and desktop publishing, a “point” is a unit of measurement equal to 1/72 of an inch. This conversion is particularly useful when programmatically setting or adjusting properties that require point values, such as row heights, column widths, font sizes, or shape dimensions, but where the initial measurement or design specification is more conveniently thought of in inches. Using InchesToPoints ensures precision and consistency in layout and formatting tasks within a workbook.
Function:
The primary function of Application.InchesToPoints is to take a numeric value representing a length in inches and return the equivalent length in points as a Single (floating-point) data type.
Syntax in xlwings:
In xlwings, you access this method through the app object, which represents the Excel application. The syntax is straightforward:
app.api.InchesToPoints(Inches)
app: Your xlwingsAppinstance. This is typically obtained withxw.App()or asxw.apps[#]when connecting to an existing instance..api: This property provides direct access to the underlying Excel object model (the COM API). It is necessary for calling this method, as xlwings does not wrap every single Excel method in its high-level API.Inches: (Required) ASingleorDoublenumber representing the length in inches that you want to convert to points.
Code Examples:
Here are practical examples demonstrating how to use Application.InchesToPoints with xlwings.
- Basic Conversion:
This example simply converts a measurement and prints the result.
import xlwings as xw
# Start a new Excel instance or connect to a running one
app = xw.App(visible=False) # Use visible=True to see Excel
# Convert 2.5 inches to points
points_value = app.api.InchesToPoints(2.5)
print(f"2.5 inches is equal to {points_value} points.")
# Output: 2.5 inches is equal to 180.0 points.
app.quit()
- Setting Row Height Based on Inches:
A common use case is setting a row’s height to a specific inch measurement.
import xlwings as xw
app = xw.App(visible=False)
wb = app.books.add()
ws = wb.sheets[0]
# Desired row height: 0.75 inches
desired_height_inches = 0.75
row_height_points = app.api.InchesToPoints(desired_height_inches)
# Set the height of the first row
ws.api.Rows(1).RowHeight = row_height_points
print(f"Set Row 1 height to {desired_height_inches} inches ({row_height_points} points).")
wb.save('row_height_example.xlsx')
app.quit()
- Setting Column Width Based on Inches:
While column width in Excel uses a different, character-based unit, this method can be part of calculations for shapes or other objects placed relative to columns. For direct cell formatting related to width/height in points, it’s applicable.
import xlwings as xw
app = xw.App(visible=True)
wb = app.books.add()
ws = wb.sheets[0]
# Create a shape and set its width based on inches
# Let's say we want a rectangle that is 2 inches wide
shape_width_inches = 2.0
shape_width_points = app.api.InchesToPoints(shape_width_inches)
# Add a rectangle shape
my_shape = ws.shapes.add_shape(
type=1, # 1 corresponds to a Rectangle
left=ws.range('C5').left,
top=ws.range('C5').top,
width=shape_width_points,
height=50 # height in points
)
my_shape.name = "MyRectangle"
print(f"Shape width set to {shape_width_inches} inches ({shape_width_points} points).")
# Keep the workbook open
input("Press Enter to close...")
app.quit()
Leave a Reply