How do I print a document 2 pages on a sheet using python?

What I do:

import uno
from com.sun.star.view.PaperOrientation import PORTRAIT as PAPER_ORIENTATION_PORTRAIT
from com.sun.star.view.PaperOrientation import LANDSCAPE as PAPER_ORIENTATION_LANDSCAPE
from com.sun.star.view.PaperFormat import A4 as PAPER_FORMAT_A4
from com.sun.star.view.DuplexMode import OFF as DUPLEX_MODE_OFF
from com.sun.star.view.DuplexMode import LONGEDGE as DUPLEX_MODE_LONGEDGE
from com.sun.star.view.DuplexMode import SHORTEDGE as DUPLEX_MODE_SHORTEDGE
from com.sun.star.beans import PropertyValue

printer = 'Canon MF650C Series UFR II(1)'

def print():
    doc = XSCRIPTCONTEXT.getDocument()
    doc.Printer = mkpvseq({
        'Name': printer,
        'PaperOrientation': PAPER_ORIENTATION_PORTRAIT,
        'PaperFormat': PAPER_FORMAT_A4,
    })
    doc.PagePrintSettings = mkpvseq({
        'PageColumns': 2,
        'PageRows': 1,
        'IsLandscape': True,
    })
    doc.printPages(mkpvseq({
        'CopyCount': 1,
        'Pages': '',
        'DuplexMode': DUPLEX_MODE_OFF,
        'PrinterName': printer,
    }))

def mkpvseq(props):
    pvs = []
    for n, v in props.items():
        pv = PropertyValue()
        pv.Name = n
        pv.Value = v
        pvs.append(pv)
    return tuple(pvs)

g_exportedScripts = print,

Basically printing works, but in this particular case it prints 2 pages on different sides of a sheet. How do I make it print 2 pages on one side of a sheet? What can I check? Any suggestions?

When you print manually and save then, your file has a config item PrinterSetup in its settings.xml sub-file. It is a base64 encoded string. That is not a printer property but a document property. The property PrinterSetup is available as API property in service css.document.Settings. Perhaps you try to use that? But I don’t know how to do it in a Python script.

I didn’t find a property there to set the number of pages per sheet of paper.
I think this property lives and dies in the print dialog.
It’s likely that this property can be set in the dialog via the XAccessible family of interfaces, but this interface is unpublished. :slight_smile:

First, what I’m doing… it looks correct to me. It should print 2 pages on one side of a sheet. But doesn’t. A bug?

What is css.document.Settings? I don’t see a css namespace. Any hints?

You can try to provide non-python code. Maybe that’ll help me figure out how to do it in python.

Sounds like a hard way. I might give it a try if anything else fails. Thanks for the suggestion anyway.

com.sun.star

doc.createInstance('com.sun.star.document.Settings')

does not include anything related to printing. Yes, looks like a bug.
https://api.libreoffice.org/docs/idl/ref/servicecom_1_1sun_1_1star_1_1document_1_1Settings.html

According to the documentation there should be at least IsPrintBooklet. But the property doesn’t exist in the returned object. And I can’t set it.

This property is described in the documentation as follows:
boolean IsPrintBooklet optional property

Try this. I can’t test it right now.

pps = doc.getPagePrintSettings()
for p in pps:
    if p.Name = 'PageColumns':
        p.Value = 2
    elif p.Name = 'PageRows':
        p.Value = 1
    elif p.Name = 'IsLandscape':
        p.Value = True
doc.setPagePrintSettiings(p)

As before, prints on different sides of a sheet.