2018年5月6日 星期日

用Libreoffice writer巨集方便整理文章

為了用LibreOffice writer整理OCR後的文章內容,到網路上(Ask LibreOffice 互助問答)詢問和搜尋,加上自己實作和推想,得到了整理文書的好用巨集,紀錄在這兒,讓有用到的人可以參考。

1.直接將「Hledat」內的字串,取代為「Nahradit」的字串: (來自Lio ooo社團
----------------
Sub Najdi_Nahrad()
    Dim oDoc,oText,oVC,oStart,oEnd,oFind,FandR
    oDoc = ThisComponent : oText = oDoc.Text
    oVC = oDoc.CurrentController.getViewCursor
        Hledat = Array("0","1", "2","3" ,"4", "5" ,"6" ,"7", "8","仿佛","却", "Search", "Value")
        Nahradit = Array("〇","一", "二", "三", "四", "五", "六", "七", "八","彷彿","卻", "hledat", "Hodnota")
        Pocet = 0
       While Pocet <= uBound(Hledat)
          oStart = oText.createTextCursorByRange(oVC.Start)
          If Not oVC.isCollapsed then oEnd = oText.createTextCursorByRange(oVC.End)
             FandR = oDoc.createReplaceDescriptor
             With FandR
                .SearchString = Hledat(Pocet)
                .ReplaceString = Nahradit(Pocet)
                .SearchWords = false
             End With
          If isEmpty(oEnd) then 'Do whole document.
               oDoc.replaceAll(FandR)
          Else
               Do
                   oFind = oDoc.FindNext(oStart.End,FandR)
                   If isNull(oFind) then Exit Do
                   If oText.compareRegionEnds(oFind,oEnd) < 0 then Exit Do
                   oFind.setString(FandR.ReplaceString)
                   oFind = oDoc.FindNext(oFind.End,FandR)
              Loop
          EndIf
       Pocet = Pocet + 1
        Wend     
End Sub
---------------
2.回到文章最前面,即〔按[Ctrl+Home〕
----------------
sub Ctrlhome1
rem -----------------------------------
rem define variables
dim document   as object
dim dispatcher as object
rem -----------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem -----------------------------------
dispatcher.executeDispatch(document, ".uno:GoToStartOfDoc", "", 0, Array())
end sub
----------------
3.找「args1(11).Value = "? "」字串
---------------
sub no01
rem -----------------------------------
rem define variables
dim document   as object
dim dispatcher as object
rem -----------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem -----------------------------------
dim args1(21) as new com.sun.star.beans.PropertyValue
args1(0).Name = "SearchItem.StyleFamily"
args1(0).Value = 2
args1(1).Name = "SearchItem.CellType"
args1(1).Value = 0
args1(2).Name = "SearchItem.RowDirection"
args1(2).Value = true
args1(9).Name = "SearchItem.AlgorithmType"
args1(9).Value = 0
args1(10).Name = "SearchItem.SearchFlags"
args1(10).Value = 0
args1(11).Name = "SearchItem.SearchString"
args1(11).Value = "? "
args1(12).Name = "SearchItem.ReplaceString"
args1(12).Value = ""
args1(13).Name = "SearchItem.Locale"
args1(13).Value = 255
args1(14).Name = "SearchItem.ChangedChars"
args1(14).Value = 2
args1(15).Name = "SearchItem.DeletedChars"
args1(15).Value = 2
args1(16).Name = "SearchItem.InsertedChars"
args1(16).Value = 2
args1(17).Name = "SearchItem.TransliterateFlags"
args1(17).Value = 256
args1(18).Name = "SearchItem.Command"
args1(18).Value = 0
args1(19).Name = "SearchItem.SearchFormatted"
args1(19).Value = false
args1(20).Name = "SearchItem.AlgorithmType2"
args1(20).Value = 1
args1(21).Name = "Quiet"
args1(21).Value = true

dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, args1())
end sub
--------------
4.執行上一個動作(如3.),再新增段落
---------------
sub fixz0
rem -----------------------------------
rem define variables
dim document   as object
dim dispatcher as object
rem -----------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem -----------------------------------
dim args1(0) as new com.sun.star.beans.PropertyValue
args1(0).Name = "Quiet"
args1(0).Value = true

dispatcher.executeDispatch(document, ".uno:RepeatSearch", "", 0, args1())

rem -----------------------------------
dim args2(1) as new com.sun.star.beans.PropertyValue
args2(0).Name = "Count"
args2(0).Value = 1
args2(1).Name = "Select"
args2(1).Value = false

dispatcher.executeDispatch(document, ".uno:GoLeft", "", 0, args2())

rem -----------------------------------
dispatcher.executeDispatch(document, ".uno:InsertPara", "", 0, Array())

rem -----------------------------------
dim args4(0) as new com.sun.star.beans.PropertyValue
args4(0).Name = "Quiet"
args4(0).Value = true

dispatcher.executeDispatch(document, ".uno:RepeatSearch", "", 0, args4())

rem -----------------------------------
dim args5(1) as new com.sun.star.beans.PropertyValue
args5(0).Name = "Count"
args5(0).Value = 1
args5(1).Name = "Select"
args5(1).Value = false

dispatcher.executeDispatch(document, ".uno:GoLeft", "", 0, args5())

rem -----------------------------------
dispatcher.executeDispatch(document, ".uno:InsertPara", "", 0, Array())
end sub
-------------------
5.刪除全文中的半型空白和全型空白
-------------------
sub bit20
rem -----------------------------------
rem define variables
dim document   as object
dim dispatcher as object
rem -----------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem -----------------------------------
dim args1(1) as new com.sun.star.beans.PropertyValue
args1(0).Name = "Count"
args1(0).Value = 1
args1(1).Name = "Select"
args1(1).Value = true

dispatcher.executeDispatch(document, ".uno:GoRight", "", 0, args1())

rem -----------------------------------
dim args2(21) as new com.sun.star.beans.PropertyValue
args2(0).Name = "SearchItem.StyleFamily"
args2(0).Value = 2
args2(1).Name = "SearchItem.CellType"
args2(1).Value = 0
args2(2).Name = "SearchItem.RowDirection"
args2(2).Value = true
args2(9).Name = "SearchItem.AlgorithmType"
args2(9).Value = 0
args2(10).Name = "SearchItem.SearchFlags"
args2(10).Value = 65536
args2(11).Name = "SearchItem.SearchString"
args2(11).Value = " "
args2(12).Name = "SearchItem.ReplaceString"
args2(12).Value = ""
args2(13).Name = "SearchItem.Locale"
args2(13).Value = 255
args2(14).Name = "SearchItem.ChangedChars"
args2(14).Value = 2
args2(15).Name = "SearchItem.DeletedChars"
args2(15).Value = 2
args2(16).Name = "SearchItem.InsertedChars"
args2(16).Value = 2
args2(17).Name = "SearchItem.TransliterateFlags"
args2(17).Value = 1280
args2(18).Name = "SearchItem.Command"
args2(18).Value = 3
args2(19).Name = "SearchItem.SearchFormatted"
args2(19).Value = false
args2(20).Name = "SearchItem.AlgorithmType2"
args2(20).Value = 1
args2(21).Name = "Quiet"
args2(21).Value = true

dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, args2())

end sub
----------------------
6.將所有半型改為全型
---------------------
sub word2bit1   '全部改為全型
rem -----------------------------------
rem define variables
dim document   as object
dim dispatcher as object
rem -----------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem -----------------------------------
dispatcher.executeDispatch(document, ".uno:GoToStartOfDoc", "", 0, Array())

rem -----------------------------------
dispatcher.executeDispatch(document, ".uno:SelectAll", "", 0, Array())

rem -----------------------------------
dispatcher.executeDispatch(document, ".uno:ChangeCaseToFullWidth", "", 0, Array())

rem -----------------------------------
dispatcher.executeDispatch(document, ".uno:GoToStartOfDoc", "", 0, Array())
end sub
 -------------------
7.除了「sString 」中的字串外,刪除段落(將下一段文字接到前段)
  參考LibreOffice正體中文使用者社團 → pastebin - Miscellany - post number 3992044
----------------
sub Main003
  Dim oCurrentController As Variant
  Dim oViewCursor As Variant
  Dim oText As Variant
  Dim oObj1 As Variant
  Dim oObj2 As Boolean
  Dim oObj3 As Boolean
  Dim sString As String

  oCurrentController = ThisComponent.getCurrentController()
  oViewCursor = oCurrentController.getViewCursor()
  oText = oViewCursor.getText()
 
  oObj1 = oText.createTextCursorByRange(oViewCursor)
  oObj1.gotoEndOfParagraph(False)
  oObj1.goLeft(1, True)
 
  sString = oObj1.getString()
  oObj1.collapseToEnd()
  oObj1.gotoEndOfParagraph(False)
  oViewCursor.gotoRange(oObj1, False)
    document   = oCurrentController.Frame
    dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")   

  if sString = "。" or sString ="?" or sString ="." or sString ="…"  or sString ="!" or sString ="x" or sString ="」"  or sString =":"  or sString ="★"  or sString ="※" then
    rem -----------------------------------
      dispatcher.executeDispatch(document, ".uno:GoToNextPara", "", 0, Array())

  else
    dispatcher.executeDispatch(document, ".uno:Delete", "", 0, Array())
   
  end if
 
end sub
-----------------------8.重複執行巨集(Main003)三次
----------------
Sub my_Procedure()
    Call Main003
    Call Main003
    Call Main003
 End Sub
-----------------
9.將以上各巨集全部合成一個巨集,由前向後執行
---------------
Sub fi2pro_all()
    Call  fire01 '取代文字

Call Ctrlhome1 '回到最前面
    Call no01       '找到"? "   
        Call find_xz       '新增段落,並重複執行

Call Ctrlhome1 '回到最前面
    Call no02      '找到"」 "
   
        Call find_xz       '新增段落,並重複執行
                Call find_xz       '新增段落,並重複執行
Call Ctrlhome1 '回到最前面
    Call no03      '找到": "
   
        Call find_xz        '新增段落,並重複執行
Call Ctrlhome1 '回到最前面
    Call no04      '找到"。 "
 
        Call find_xz       '新增段落,並重複執行
                Call find_xz      '新增段落,並重複執行

Call Ctrlhome1 '回到最前面
    Call no05      '找到"! "
 
        Call find_xz       '新增段落,並重複執行

Call Ctrlhome1 '回到最前面
    Call no06      '找到"』 "則換一段
 
        Call find_xz       '新增段落,並重複執行
               
Call Ctrlhome1 '回到最前面
Call bit20        '刪去空白(包括全型半型)
Call word2bit1          '全部改為全型符號
Call Ctrlhome1       
Call my_Procedure        '接斷行 
    end sub
--------------

沒有留言:

張貼留言