Can I use Python to retrieve or modify UNO commands for this? I don’t want to change it globally, as different users have different unit preferences.
How to set the paragraph indentation unit to points or other units instead of centimeters in Writer?
hii @honliideal
You can use Python via UNO to get or set measurement units per user or per document, without changing them globally.
Example (global read):
config = ctx.ServiceManager.createInstanceWithContext(
"com.sun.star.configuration.ConfigurationProvider", ctx
)
Example (per-document):
view = XSCRIPTCONTEXT.getDocument().getCurrentController().getViewSettings()
view.setPropertyValue("Metric", 2) # 1=cm, 2=mm, 3=inch
ViewSettings.Metric affects only the current document view.
Use ConfigurationAccess only for reading global preferences.
Thank you for the reply, but I wrote a unit test and it threw an error immediately.
I checked the ViewSettings object using LibreOffice’s development tools, and it doesn’t have any Metric-related properties either.
def TestSetMetric():
view = XSCRIPTCONTEXT.getDocument().getCurrentController().getViewSettings()
\# 1=cm, 2=mm, 3=inch
view.setPropertyValue("Metric", 2)
‘ooo_script_framework.com.sun.star.beans.UnknownPropertyException’>: Metric
File “D:\soft\LibreOffice\program\pythonscript.py”, line 921, in invoke
ret = self.func( *args )
File “D:\soft\LibreOffice\share\Scripts\python\honlitechpy\honlitech-frontend-colla\scripts\writer\Debug.py”, line 548, in TestSetMetric
view.setPropertyValue(“Metric”, 2)
))
LibreOffice (and thus UNO) internally uses 1/100 mm (hundredths of a millimeter) for all layout and formatting metrics.
That’s why you can ignore user UI units entirely when using UNO: the document model always stores and expects values in that fixed internal unit.
So:
| User value | Conversion | UNO value to set |
|---|---|---|
| 1.27 cm | 12.7 mm × 100 | 1270 |
| 10 pt ≈ 3.528 mm | 3.528 × 100 | ≈ 353 |
| 1 inch | 25.4 mm × 100 | 2540 |
You don’t need to read or set user display units for formatting purposes.
Just:
- Convert user-facing units → 1/100 mm,
- Set the UNO property (e.g.
ParaLeftMargin,ParaFirstLineIndent, etc.).
That ensures your macro or automation behaves consistently for all users, independent of their personal unit preferences.
Got it. The previous code still couldn’t change the unit, and I noticed that this is related to the user’s current locale. The development team should provide an interface allowing users to choose their preferred unit, as everyone has different habits.