LibreOffice UNO:设置样式(可以使用 Java、VB、Python、C++、任何使用 UNO API 的语言提供)

Posted

技术标签:

【中文标题】LibreOffice UNO:设置样式(可以使用 Java、VB、Python、C++、任何使用 UNO API 的语言提供)【英文标题】:LibreOffice UNO: Setting Styles (anwer can be provided in Java, VB, Python, C++, any language using UNO API) 【发布时间】:2014-02-20 04:57:43 【问题描述】:

我有一个问题,我只是尝试添加新文本,然后对其应用 LibreOffice 样式。我想添加文本并使其遵循特定样式(“标题 1”、“标题 2”等)。

向文档添加文本确实有效,更改样式确实有效,但是最后一个样式集应用于整个文档,而不仅仅是最后一个字符串。我需要一些方法来限制对字符串的选择。我想我需要一个 XTextRange 并且 Style 属于那个而不是光标......但不知道如何创建只包含我最新的 String 的新 XTextRanges......显然不确定,并且建议将是最受欢迎的。

编辑:虽然以下代码是 Java 代码,但我非常愿意接受使用任何编程语言的解决方案,但 UNO API 非常相似,我可以将解决方案从另一种语言转换。我感觉面向 OOo/LO 的 VB 宏编写器比 Java 开发人员多,那么也许 C++ 或 Python 开发人员有解决方案。我应该认为写出一个更改样式的文档将是一个非常基本的要求!

com.sun.star.text.XText xText = this.xDoc.getText();
//create a cursor object
com.sun.star.text.XTextCursor xTCursor = xText.createTextCursor();
this.writeResume(xText, xTCursor);

写简历的方法...你会看到我尝试用 changeStyle 方法改变样式的地方

private void writeResume(com.sun.star.text.XText xText, com.sun.star.text.XTextCursor xTCursor) 
    TestData resume = new TestData();
    List<Company> companies = resume.getCompanies();
    this.changeStyle(xTCursor, "Heading 1");
    xText.insertString(xTCursor, "Professional Experience\n", false);
    xTCursor.collapseToEnd();
    this.changeStyle(xTCursor, "Heading 2");
    Company company = companies.get(0);
    String date = dformat.format(company.getStartDate().getTime()) + " - " + dformat.format(company.getEndDate().getTime());
    xText.insertString(xTCursor, company.getName() + "," + company.getLocation() + "\t" + date + "\n", false);
    xTCursor.collapseToEnd();
    this.changeStyle(xTCursor, "Heading 3");
    xText.insertString(xTCursor, "Test Point 1\n", false);
    xText.insertString(xTCursor, "Test Point 2\n", false);
    xText.insertString(xTCursor, "Test Point 3\n", false);

changeStyle 方法

public void changeStyle(com.sun.star.text.XTextCursor xTCursor, String styleName) 
    XPropertySet xCursorProps = UnoRuntime.queryInterface(XPropertySet.class, xTCursor);
    try 
        xCursorProps.setPropertyValue("ParaStyleName", styleName);
     catch (UnknownPropertyException | PropertyVetoException | IllegalArgumentException | WrappedTargetException ex) 
        Logger.getLogger(ResumeWriter.class.getName()).log(Level.SEVERE, null, ex);
    

【问题讨论】:

【参考方案1】:

通过反复试验解决了问题:

要点:

需要使用“\r”而不是“\n” 需要比预期更频繁地调用我的设置样式方法 在再次查看此页面后发现上述内容:https://wiki.openoffice.org/wiki/Documentation/DevGuide/Text/Editing_Text

仅更改了以下方法:

private void writeResume(com.sun.star.text.XText xText, com.sun.star.text.XTextCursor xTCursor) 
    TestData resume = new TestData();
    List<Company> companies = resume.getCompanies();
    //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    this.changeStyle(xTCursor, "Heading 1");//if this line is NOT here then will default to a custom style
    xText.insertString(xTCursor, "Professional Experience\n\r", false);
    xTCursor.collapseToEnd();
    //xText.insertControlCharacter(xText, com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, false);
    this.changeStyle(xTCursor, "Heading 2");//if this line is NOT here then will default to a custom style
    Company company = companies.get(0);
    String date = dformat.format(company.getStartDate().getTime()) + " - " + dformat.format(company.getEndDate().getTime());
    xText.insertString(xTCursor, company.getName() + "," + company.getLocation() + "\t" + date + "\r", false);
    xText.insertString(xTCursor,"Title\r", false);

    this.changeStyle(xTCursor, "Heading 3");//if this line is NOT here then will default to a custom style
    xText.insertString(xTCursor, "Test Point 1\r", false);
    this.changeStyle(xTCursor, "Heading 3");
    xText.insertString(xTCursor, "Test Point 2\r", false);
    this.changeStyle(xTCursor, "Heading 3");
    xText.insertString(xTCursor, "Test Point 3\r", false);

【讨论】:

嗯……我使用“\n”没有问题。

以上是关于LibreOffice UNO:设置样式(可以使用 Java、VB、Python、C++、任何使用 UNO API 的语言提供)的主要内容,如果未能解决你的问题,请参考以下文章

LibreOffice API/UNO:如何在编写器的表格单元格中水平右对齐文本

如何使用 UNO 遍历 OpenOffice/LibreOffice 中的整个文档

Libreoffice API (UNO):需要更改用户的 xTextField 文本

如何在 Python + Windows 中使用 LibreOffice API (UNO)?

Python UNO(libreoffice):如何为工作表启用自动过滤器

无法在 python 中为 ubuntu 16.04 上的 libreoffice 导入 uno