是否可以将.ods和.pdf文件的文本复制到libreoffice .odt文件中?

问题描述

我正在尝试创建一个libreoffice基本宏,该宏可让您将文件的全部内容复制到表行中。下面的代码可与文本文件(例如.ods或.txt)正常工作,但.pdf和.ods文件存在一些问题。特别是,它在getText()方法上崩溃。 您知道我可以使用其他任何方法解决我的问题吗?

`

REM ***The file Url***
sUrlDoc = "file:///C:/Users/user/Desktop/Test.ods"

REM ***It correctly opens the file***
odoc = StarDesktop.loadComponentFromURL(sUrlDoc,"_blank",Prop() )

REM ***Correctly inserts a new row in the table***
oTable.Rows.insertByIndex(oTable.getRows().getCount(),1)

REM ***It goes into the right position***
oCell = oTable.getCellByPosition(0,1)

REM ***Should read from file (only works with .odt and .txt)***
oCursor = odoc.getText(1)
oCell.setString(oCursor.string)

odoc.close(true)`

解决方法

您可以通过多种方式获取ODS文件的上下文。

最慢的方法是逐页逐个单元格地访问工作簿中的所有数据,取出每个单元格的文本内容。

我建议使用Andrew Pitonyak5.23. Manipulating the clipboard章中显示的方法(请紧紧握住本书,您不必编写许多宏来解决日常任务,您只需采取现成的代码

Function getContentODS(sDocName As String) As String 
Dim oDoc As Variant         ' Spreadsheet as object
Dim bDisposable As Boolean  ' Can be closed
Dim oSheets As Variant      ' All sheets of oDoc
Dim oSheet As Variant       ' Single sheet
Dim i As Long           
Dim oCurrentController As Variant
Dim oCursor As Variant      ' Get Used Area
Dim oTransferable As Variant    ' Content of selection
Dim oTransferDataFlavors As Variant
Dim oConverter As Variant   ' Util
Dim j As Integer,iTextLocation As Integer
Dim oData As Variant
Dim sResult As String       ' All content as very long string
    GlobalScope.BasicLibraries.loadLibrary("Tools")
    If Not FileExists(sDocName) Then Exit Function 
    oDoc = OpenDocument(ConvertToURL(sDocName),Array(),bDisposable)
    sResult = FileNameoutofPath(sDocName) & ": "
    oCurrentController = oDoc.getCurrentController()
    oSheets = oDoc.getSheets()
    oConverter = createUnoService("com.sun.star.script.Converter")
    For i = 0 to oSheets.getCount()-1
        oSheet = oSheets.getByIndex(i)
        oCursor = oSheet.createCursor()
        oCursor.gotoEndOfUsedArea(True)
        oCurrentController.select(oCursor)
        oTransferable = oCurrentController.getTransferable()
        oTransferDataFlavors = oTransferable.getTransferDataFlavors()
        iTextLocation = -1
        For j = LBound(oTransferDataFlavors) To UBound(oTransferDataFlavors)
            If oTransferDataFlavors(j).MimeType = "text/plain;charset=utf-16" Then
                iTextLocation = j
                Exit For
            End If
        Next
        If (iTextLocation >= 0) Then
            oData = oTransferable.getTransferData(oTransferDataFlavors(iTextLocation))
            sResult = sResult & oSheet.getName() & "=" & _
                oConverter.convertToSimpleType(oData,com.sun.star.uno.TypeClass.STRING) & "; "
        End If
    Next i
    If bDisposable Then oDoc.close(True)
    getContentODS = sResult
End Function

此函数将打开电子表格,该电子表格将在参数中接收其路径和名称,遍历所有工作表,取出文本内容并将其连接为一个长字符串变量,最后关闭文档

您可以使用以下过程测试此代码:

Sub tst
    MsgBox getContentODS("C:\Users\user\Desktop\Test.ods")
End Sub

因此该函数将为您返回一个字符串。考虑如何处理这一行(或参阅第7章。Writer宏)

要获取PDF文档的文本部分,可以使用类似的技术(从AcrobatReader复制内容到剪贴板,仅取出复制的文本部分),或在Draw中打开它并遍历所有图形元素为了从中提取文本片段。