【问题标题】:Is there a way to copy the text of an .ods and .pdf file into a libreoffice .odt file?有没有办法将 .ods 和 .pdf 文件的文本复制到 libreoffice .odt 文件中?
【发布时间】:2020-10-07 07:48:34
【问题描述】:

我正在尝试创建一个 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", 0, 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)`

【问题讨论】:

  • 欢迎来到 SO!请告诉我们更多关于你在做什么。有一种方法可以复制电子表格的内容。 PDF 文件可能会出现问题 - 并非所有此类文件都包含文本,有些可能包含图像、文档扫描件。告诉我们要解决的问题——为什么要将文件内容复制到表格行中?
  • 嗨,John,好吧,基本上我两天前就开始创建宏了,所以我是语言新手。我的老板问我是否可以想办法将用户可以选择的其他文件的全部内容(这些文件只包含文本)插入到 .ods 文件中。我使用户可以插入要包含的文件列表,并记下路径。因此,单击按钮应显示所选文件的全部内容。本练习的最终目的是在打印时将这些文件显示在“较大的文件”中。
  • “我两天前就开始创建宏了” 哦,欢迎来编程!我不太明白你老板的想法,但问题是可以解决的。请澄清 - 结果将累积在 ODT 文本文档(如问题标题中所述)或 ODS 电子表格中(如您在评论中指出的那样)?
  • 对不起,如果我看起来有点混乱:我的最终目的是将 ODS 电子表格的全部内容复制到 ODT 文件中。我老板的意图是最终获得一个ODT文件,其中包含来自不同ODS文件的文本。

标签: libreoffice libreoffice-calc libreoffice-basic libreoffice-writer


【解决方案1】:

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

其中最慢的是逐个工作表和逐个单元格地迭代工作簿中的所有数据,取出每个单元格的文本内容。

我建议使用Andrew Pitonyak 在章节5.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 Macros)

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

【讨论】:

  • 谢谢,这正是我想要的!
猜你喜欢
  • 1970-01-01
  • 2010-12-06
  • 2015-07-15
  • 1970-01-01
  • 1970-01-01
  • 2019-02-11
  • 1970-01-01
  • 1970-01-01
  • 2021-10-24
相关资源
最近更新 更多