LibreOffice - 如何通过 python 宏创建文件对话框?

Posted

技术标签:

【中文标题】LibreOffice - 如何通过 python 宏创建文件对话框?【英文标题】:LibreOffice - How to create a file dialog via python macro? 【发布时间】:2015-06-15 08:38:49 【问题描述】:

我想知道是否可以通过 python 宏创建一个标准文件对话框来保存 pdf。我试图根据这个过时的文档编写一些代码:wiki.openoffice.org 但 LibreOffice 在执行后崩溃:

import os
import uno
import sys
import traceback
from com.sun.star.ui.dialogs.TemplateDescription import FILESAVE_SIMPLE

def file_dialog():
    try:
        oCtx = uno.getComponentContext()
        oServiceManager = oCtx.getServiceManager()

        oFilePicker = oServiceManager.createInstanceWithArgumentsAndContext(
                'com.sun.star.ui.dialogs.FilePicker',
                (FILESAVE_SIMPLE,),
                oCtx
            )

        oFilePicker.Title = 'Export as'

        #oDisp = oFilePicker.Text

        oFilePicker.execute()

    except:
        pass
        #oDisp = traceback.format_exc(sys.exc_info()[2])

最后我需要通过选择的路径来写文档,但是oDisp = oFilePicker.Text返回:(<type 'exceptions.AttributeError'>。另外有没有办法设置文件类型?

有人有这方面的经验吗?

【问题讨论】:

【参考方案1】:

我在 oFilePicker 对象上使用了Xray。有几个有趣的方法称为 setCurrentFilter 和 appendFilterGroup。仅根据名称,它们可用于过滤可见的文件类型。不幸的是,我不确定如何使用它们。

同样使用 Xray,我确定 Text 不是 oFilePicker 对象的方法或属性。我不确定代码 sn-p 在那里尝试做什么?如果检索文件路径,1) 需要在 .execute 之后完成,2) 选定的文件路径存储为字符串数组,因此必须从数组中提取路径。我在 OpenOffice 中的大部分工作都在 StarBasic 中。下面是打印用户选择的文件路径的基本示例:

Sub TestFilePicker
    oFilePickerDlg = createUnoService( "com.sun.star.ui.dialogs.FilePicker" )
    oFilePickerDlg.setTitle("My test title")

    If oFilePickerDlg.execute() > 0 Then
        Print ConvertFromURL(oFilePickerDlg.Files(0))
    End If

End Sub

【讨论】:

感谢您的回答 Lyrl +1,但不幸的是我正在寻找 python 解决方案。 oDisp = 行移到.execute之后。这是我 100% 确定的。然后将.Text 更改为.Files(0) - 这部分我有点不确定,我不确定python如何调用数组。但是,作为 python 用户,您肯定会理解其意图是将名为 Files 的数组中的第一个条目分配给变量 oDisp,并且会知道正确的 python 代码。【参考方案2】:

给出并接受答案(因为问题被交叉发布了!)here:

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

#shortcut:
createUnoService = (
        XSCRIPTCONTEXT
        .getComponentContext()
        .getServiceManager()
        .createInstance 
                    )

def pypdf_test():

    desktop = XSCRIPTCONTEXT.getDesktop()
    doc = desktop.getCurrentComponent()

    # filter data
    fdata = []
    fdata1 = PropertyValue()
    fdata1.Name = "SelectPdfVersion"
    fdata1.Value = 1
    fdata2 = PropertyValue()
    fdata2.Name = "Quality"
    fdata2.Value = 100
    fdata.append(fdata1)
    fdata.append(fdata2)

    args = []
    arg1 = PropertyValue()
    arg1.Name = "FilterName"
    arg1.Value = "writer_web_pdf_Export"
    arg2 = PropertyValue()
    arg2.Name = "FilterData"
    arg2.Value = uno.Any("[]com.sun.star.beans.PropertyValue", tuple(fdata) )
    args.append(arg1)
    args.append(arg2)
    fileurl = FilePicker()
    if fileurl:        
        doc.storeToURL( fileurl, tuple(args) )

def FilePicker(path=None, mode=1):
    """
    Datei öffnen:  `mode in (0, 6, 7, 8, 9)`
    Datei Schreiben `mode in (1, 2, 3, 4, 5, 10)`
    see: ('''http://api.libreoffice.org/docs/idl/ref/
            namespacecom_1_1sun_1_1star_1_1ui_1_1
            dialogs_1_1TemplateDescription.html''' )
    """

    filepicker = createUnoService( "com.sun.star.ui.dialogs.OfficeFilePicker" )
    if path:
        filepicker.setDisplayDirectory(path )
    filepicker.initialize( ( mode,) )
    if filepicker.execute():
        return filepicker.getFiles()[0]  

【讨论】:

通知 Karolus - 他同意了。如果您对代码进行更多解释,尤其是对于非德国用户这里,我会接受并赞成您的回答。 解释什么?您的问题是“如何调用内置的FileDialog,用于 PDF 导出。这段代码显示了 PDF 属性和过滤器的设置,调用FileDialog 并返回给定的名称(如果没有取消)。这个版本是写的适用于 LibreOffice,但也适用于 Apache OpenOffice。 这不是一个论坛,在这里解释代码的工作原理是一种很好的做法。引用:We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed. 我认为如果做交叉发帖不提供参考是不礼貌的。您问了多少其他网站/论坛而没有指出?代码是 Python = 可读的英语。如果您有不明白的地方问。 不要将其视为个人 - 您的代码包含德语,请参阅FilePicker() 函数。再一次 - 这里的其他用户至少可以评论代码真的很好,请参阅上面引用中的 *** 规则。如果这对你来说太复杂了,我可以编辑你的答案。

以上是关于LibreOffice - 如何通过 python 宏创建文件对话框?的主要内容,如果未能解决你的问题,请参考以下文章