Ask Your Question

Set image to original size with Python macro

asked 2017-12-11 04:13:39 +0200

reuben gravatar image

updated 2017-12-11 12:40:35 +0200

I am embedding an image in a writer document using Python3. I just can't figure out how to set the image to the original size.

import uno
import unohelper

# a UNO struct later needed to create a document
from import AS_CHARACTER
from import Size

localContext = uno.getComponentContext()

resolver = localContext.ServiceManager.createInstanceWithContext(
                "", localContext )

smgr = resolver.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ServiceManager" )
remoteContext = smgr.getPropertyValue( "DefaultContext" )

desktop = smgr.createInstanceWithContext( "",remoteContext)

# open a writer document
doc = desktop.loadComponentFromURL( "private:factory/swriter","_blank", 0, () )

text = doc.Text
cursor = text.createTextCursor()
text.insertString( cursor, "The first line in the newly created text document.\n", 0 )
text.insertString( cursor, "Now we are in the second line\n" , 0 )

# create a text table
table = doc.createInstance( "" )

# with 4 rows and 4 columns
table.initialize( 4,4)

text.insertTextContent( cursor, table, 0 )
rows = table.Rows

img = doc.createInstance('') 
img.GraphicURL = 'file:///home/reuben/reuben.jpg' 
tableText = table.getCellByName( "C2" )
cursor = tableText.createTextCursor()
tableText.insertTextContent(cursor, img, False)
img.setPropertyValue('AnchorType', AS_CHARACTER)

Any suggestions? Thanks.

[Edit] LO skews the image size sometimes, and I want it to be the original size. The problem is I don't know what the original size was.

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2017-12-11 05:17:17 +0200

Ratslinger gravatar image


Not sure about the rest of you code, but here is how to set the size:

imageSize = Size(2000,1500)

where 2000 is width and 1500 is width (the size in 100/th mm ).

If this answers your question please click on the ✔ (upper left area of answer).

edit flag offensive delete link more


Thanks, your answer was the other thing I was confused about. But anyway, I want to set the image to the original size. You know, the size it was taken on the camera. Or should I say the size it shows up on the monitor when opened for the first time. See my edit.

reuben gravatar imagereuben ( 2017-12-11 12:30:48 +0200 )edit

This is what I was looking for:

w = img.ActualSize.Width
h = img.ActualSize.Height
size = Size (w, h)
img.setSize (size)
reuben gravatar imagereuben ( 2017-12-11 14:17:42 +0200 )edit

@reuben Thanks for posting the code. My fault for not being specific on "ActualSize".

Ratslinger gravatar imageRatslinger ( 2017-12-11 17:49:28 +0200 )edit

@Ratslinger That's fine. Your answer pointed me in the right direction. Thanks again.

reuben gravatar imagereuben ( 2017-12-11 18:10:21 +0200 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2017-12-11 04:13:39 +0200

Seen: 244 times

Last updated: Dec 11 '17