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