Macro in Python: how to set a FilterData to export a single sheet to pdf? calc

I have a calc file with 5 sheets, I have a button on sheet 2 I want to run a macro from this button to export sheet 5 to pdf. I’ve tried several ways and they all export all sheets to pdf

import uno
from pathlib import Path
from com.sun.star.beans import PropertyValue
from com.sun.star.table import CellRangeAddress

def create_instance(name, with_context=False):
    CTX = XSCRIPTCONTEXT.getComponentContext()
    SM = CTX.ServiceManager

    if with_context:
        instance = SM.createInstanceWithContext(name, CTX)
    else:
        instance = SM.createInstance(name)
    return instance


def call_dispatch(doc, url, args=()):
    frame = doc.getCurrentController().getFrame()
    dispatch = create_instance('com.sun.star.frame.DispatchHelper')
    dispatch.executeDispatch(frame, url, '', 0, args)
    return


def set_property_value(**kwargs):
    props = []
    for key in kwargs:
        #prop = uno.createUnoStruct('com.sun.star.beans.PropertyValue')
        prop = PropertyValue() #from com.sun.star.beans import PropertyValue
        prop.Name = key
        prop.Value = kwargs[key]
        props.append(prop)
    return tuple(props)


def to_pdf(*args):
    doc = XSCRIPTCONTEXT.getDocument()
    sheets = doc.Sheets
    
    fn_output = 'test0.pdf'
    this_doc_path = Path(uno.fileUrlToSystemPath(doc.URL)).parent
    file_path_output = Path(this_doc_path, fn_output)
    out_pdf = uno.systemPathToFileUrl(str(file_path_output))

    #tp_1 = set_property_value(ToPoint='$A$1:$G$10')
    #call_dispatch(doc, '.uno:GoToCell', tp_1)


    #don't work: export all sheets
    """ mySheet = sheets['test']
    myRange = mySheet['A1:C5']
    tp_FilterData = set_property_value(Selection=myRange)
    pdf_props = set_property_value(URL=out_pdf, FilterName='calc_pdf_Export', FilterData=tp_FilterData)
    call_dispatch(doc, '.uno:ExportToPDF', pdf_props) """


    #don't work: export all sheets
    """ tp_FilterData = set_property_value(Selection=1)
    pdf_props = set_property_value(URL=out_pdf, FilterName='calc_pdf_Export', FilterData=tp_FilterData)
    call_dispatch(doc, '.uno:ExportToPDF', pdf_props) """


    #don't work: export all sheets
    """ tp_FilterData = set_property_value(PageRange='1')
    pdf_props = set_property_value(URL=out_pdf, FilterName='calc_pdf_Export', FilterData=tp_FilterData)
    call_dispatch(doc, '.uno:ExportToPDF', pdf_props) """


    #don't work: export all sheets
    """ tp_FilterData = set_property_value(PageRange='1')
    pdf_props = set_property_value(FilterName='calc_pdf_Export', FilterData=tp_FilterData)
    doc.storeToURL (out_pdf, pdf_props) """

    return

This way it only exports the current sheet, but displays a window to manually configure the pdf options.

def to_pdf2(*args):
    doc = XSCRIPTCONTEXT.getDocument()
    sheets = doc.Sheets
    
    fn_output = 'test2.pdf'
    this_doc_path = Path(uno.fileUrlToSystemPath(doc.URL)).parent
    file_path_output = Path(this_doc_path, fn_output)
    out_pdf = uno.systemPathToFileUrl(str(file_path_output))

    #exports only the current sheet but displays a window to manually select pdf options
    pdf_props = set_property_value(URL=out_pdf, FilterName='calc_pdf_Export')
    call_dispatch(doc, '.uno:ExportToPDF', pdf_props)
    return

note: I use Linux mint 20.3 cinnamon

Version: 7.3.3.2 / LibreOffice Community
Build ID: 30(Build:2)
CPU threads: 8; OS: Linux 5.4; UI render: default; VCL: gtk3
Locale: pt-BR (pt_BR.UTF-8); UI: pt-BR
Ubuntu package version: 1:7.3.3~rc2-0ubuntu0.20.04.1~lo1
calc: threaded

Make sure you have correctly defined your print range in your sheet.

import uno
from com.sun.star.beans import PropertyValue

def dict_to_property(values: dict, uno_any: bool=False):
    ps = tuple([PropertyValue(Name=n, Value=v) for n, v in values.items()])
    if uno_any:
        ps = uno.Any('[]com.sun.star.beans.PropertyValue', ps)
    return ps

def main():
    path = '/home/mau/test.pdf'
    options = {'PageRange': '5'}

    doc = XSCRIPTCONTEXT.getDocument()

    filter_name = 'calc_pdf_Export'
    filter_data = dict_to_property(options, True)
    filters = {
        'FilterName': filter_name,
        'FilterData': filter_data,
    }

    path_pdf = uno.systemPathToFileUrl(path)
    args = dict_to_property(filters)

    doc.storeToURL(path_pdf, args)

    return

Unfortunately it didn’t work. This way you are exporting the 5th of all pages from all sheets. Example, all my worksheets have a total of 39 pages, so create the pdf with only the 5th page

I need to export all pages from sheet: ‘5’

Again, make sure you have your print ranges correct, then, set the range of pages you want to print.

@elmau With your help I was able to do what I needed.
Thank you so much

import uno
from com.sun.star.beans import PropertyValue

def dict_to_property(values: dict, uno_any: bool=False):
    ps = tuple([PropertyValue(Name=n, Value=v) for n, v in values.items()])
    if uno_any:
        ps = uno.Any('[]com.sun.star.beans.PropertyValue', ps)
    return ps

def main():
    doc = XSCRIPTCONTEXT.getDocument()
    sheets = doc.Sheets

    path = '/home/mau/test.pdf'

    mySheet = sheets['test5']
    options = {'Selection': mySheet}
    #options = {'Selection': mySheet['A1:C5']} #or so to set a range on the sheet

    

    filter_name = 'calc_pdf_Export'
    filter_data = dict_to_property(options, True)
    filters = {
        'FilterName': filter_name,
        'FilterData': filter_data,
    }

    path_pdf = uno.systemPathToFileUrl(path)
    args = dict_to_property(filters)

I used ‘Selection’ instead of ‘PageRange