【问题标题】:libre office macro find replace formatted textlibre office 宏查找替换格式化文本
【发布时间】:2023-03-18 01:07:01
【问题描述】:

我想浏览一个文档并找到所有居中对齐的文本并将其删除,我可以在查找和替换工具上设置格式化文本,但是当我记录时,它不保存格式...有谁知道怎么做编辑基本代码来做到这一点? 也是与libre office兼容的open office文档。

【问题讨论】:

  • 您使用的是哪个操作系统?作为应用程序内脚本的替代方法,您还可以使用 AutoHotkey 等工具。我们使用该语言自动化了许多任务。如果在不用于其他工作或目的的计算机上使用它,它的效果特别好,因此坐标不会改变。 AutoHotkey 带有一个鼠标和键盘的录制工具。
  • OSX 10.9.5,听起来很难看,但我已经使用过类似的产品。我会更喜欢完整的 libre 办公解决方案或将其导出为 html,并用一些东西解析它,因为我想在很多文件上运行它。

标签: uno libreoffice-basic


【解决方案1】:

在 OpenOffice 中录制会生成调度程序代码,这通常不是很好。编写宏时最好使用 UNO API。下面是一些你想要的代码:

Sub DeleteCenteredLines
    oDoc = ThisComponent
    Dim vDescriptor, vFound
    ' Create a descriptor from a searchable document.
    vDescriptor = oDoc.createSearchDescriptor()
    ' Set the text for which to search and other 
    With vDescriptor
      .searchString = ""
      .searchAll=True
    End With
    Dim srchAttributes(0) As New com.sun.star.beans.PropertyValue
    srchAttributes(0).Name = "ParaAdjust"
    srchAttributes(0).Value = com.sun.star.style.ParagraphAdjust.CENTER
    vDescriptor.SetSearchAttributes(srchAttributes())
    ' Find the first one
    vFound = oDoc.findFirst(vDescriptor)
    Do While Not IsNull(vFound)
        vFound.setPropertyValue("ParaAdjust", com.sun.star.style.ParagraphAdjust.LEFT)
        oTC = oDoc.Text.createTextCursorByRange(vFound)
        oTC.gotoStartOfParagraph(false)
        oTC.gotoEndOfParagraph(true)
        oTC.String = ""
        oTC.goRight(1,true)
        oTC.String = ""
        vFound = oDoc.findNext( vFound.End, vDescriptor)
    Loop
End Sub

查看http://www.pitonyak.org/AndrewMacro.odt 了解许多常见任务的示例。根据我的经验,在本文档中查找示例通常比尝试录制宏并理解所录制的内容更容易。

这适用于 OpenOffice 和 LibreOffice。通常两者的 API 是相同的。

【讨论】:

  • 非常感谢,我回家后会查看该文件。现在好像很清楚了!
  • +1 调整宏以将所有点大小 10 更改为点大小 12 的任何线索?我无法使用查找和替换来记录宏以持续工作。
  • 这听起来与这个问题有很大不同,所以你可能应该打开一个新问题。如果看起来相关,请添加指向此问题的链接。
【解决方案2】:

我的解决方案将斜体和上标字符串替换为标签。 (它非常慢。也许有人可以改进它)

Sub replace_italico_sobrescrito_por_tag()
MsgBox "It takes long to run."
Dim vartemp As String
theDoc = thisComponent
iSheetsCount = theDoc.Sheets.Count
Dim theCell As Object, rText As String, textSlice As String, textItalic As Long, textSup As Integer
Dim theParEnum As Object, theParElement As Object
Dim theSubEnum As Object, theSubElement As Object
For k=0 to iSheetsCount-1
   Sheet = theDoc.getSheets().getByIndex(k)
   dim pX as integer, pY as integer, maxcol as integer, maxrow as integer
   maxcol = 100
   maxrow = 500
   For pX=0 to maxrow
       For pY=0 to maxcol
            theCell = Sheet.GetCellByPosition(pX, pY)
           theParEnum = theCell.GetText().CreateEnumeration
            rText = ""
            Do While theParEnum.HasMoreElements
                theParElement = theParEnum.NextElement
                theSubEnum = theParElement.CreateEnumeration
                Do While   theSubEnum.HasMoreElements
                    textSlice = ""
                    theSubElement = theSubEnum.NextElement
                    If   theCell.Type = 2 Then
                        textSlice = theSubElement.String
                        textItalic = theSubElement.CharPosture
                        textSup = theSubElement.CharEscapement
                    Else
                        textSlice = theCell.String
                    End If
                    If   theSubElement.CharPosture >= 1 Then
                            textSlice = "<i>" & textSlice & "</i>"
                    End If
                    If   theSubElement.CharEscapement > 0 Then
                        textSlice = "<sup>" & textSlice & "</sup>"
                    End If
                rText = rText & textSlice   
                Loop
             Loop
             theCell.String=rText
       Next pY
   Next pX  
Next k
MsgBox "End"
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-29
    • 1970-01-01
    相关资源
    最近更新 更多