Re: paste html using API

2023-03-27 Thread Zorg

Hello,

there is two problems here

1/ the api of python does not seem to let me use 
com.sun.star.script.Converter


2/ the main problem is to insert html content in odt keeping the format 
so convert to string won't do it


sorry

Le 27/03/2023 à 13:15, sos a écrit :

We do it with the functions below

Function ClipboardAsText As String
Dim oClip As Variant, oClipContents As Variant, oTypes As Variant
Dim oConverter, convertedString As String
Dim i As Integer, iPlainLoc As Integer
iPlainLoc = -1
oClip = 
createUnoService("com.sun.star.datatransfer.clipboard.SystemClipboard")

oConverter = createUnoService("com.sun.star.script.Converter")
oClipContents = oClip.getContents()
oTypes = oClipContents.getTransferDataFlavors()
Dim msg As String
msg = "Sorry no usefull text in the ClipBoard found"
For i=LBound(oTypes) To UBound(oTypes)
If oTypes(i).MimeType = "text/plain;charset=utf-16" Then
iPlainLoc = i
Exit For
End If
Next i
If iPlainLoc >= 0 Then
convertedString = 
oConverter.convertToSimpleType(oClipContents.getTransferData(oTypes(iPlainLoc)), 
com.sun.star.uno.TypeClass.STRING)

ClipboardAsText=convertedString
End If
End Function

Sub CopyToClipBoard( sText )
' create SystemClipboard instance
oClip = CreateUnoService( _
"com.sun.star.datatransfer.clipboard.SystemClipboard")
oTR = createUnoListener("Tr_", _
"com.sun.star.datatransfer.XTransferable")
' set data
oClip.setContents(oTR,Null)
sTxtCString = sText
'oClip.flushClipboard() ' does not work
End Sub

Function Tr_getTransferData(aFlavor as 
com.sun.star.datatransfer.DataFlavor)
' wordt pas aangeroepen als we cntrol+V doen intss is sTxtCString 
reeds gezet

If (aFlavor.MimeType = "text/plain;charset=utf-16") Then
Tr_getTransferData() = sTxtCString
End If
End Function

Function Tr_getTransferDataFlavors()
Dim aFlavor As new com.sun.star.datatransfer.DataFlavor
aFlavor.MimeType = "text/plain;charset=utf-16"
aFlavor.HumanPresentableName = "Unicode-Text"
Tr_getTransferDataFlavors() = array(aFlavor)
End Function

Function Tr_isDataFlavorSupported( _
aFlavor as com.sun.star.datatransfer.DataFlavor) as Boolean
If aFlavor.MimeType = "text/plain;charset=utf-16" Then
Tr_isDataFlavorSupported = true
Else
Tr_isDataFlavorSupported = false
End If
End Function

hope it helps

Fernand

On 26/03/2023 22:26, Zorg wrote:


Thanks

Ok  it seem a goot way

maybe it is obvious but how can i paste the clipboard in my odt 
without using dispatch




Le 26/03/2023 à 21:20, Caolán McNamara a écrit :

On Sun, 2023-03-26 at 12:06 +0200, Zorg wrote:

... We have try using XTransferable but without any success.

I think it should be possible in headless mode to use the clipboard
apis to copy and paste within LibreOffice. But in this case the
clipboard is basically a fake clipboard only for the headless
libreoffice instance, one which doesn't interact with any real system
clipboard.

So for example in your current code I see that copy_with_format copies
into a true X clipboard via xclip so in headless more we can't paste
from there and it has to be non headless to get that to work.

But if the LibreOffice apis to put something into the clipboard are
used then I think it should work to "Paste" within LibreOffice and get
that content back out, even in headless more.

f.e. in this example at
https://ask.libreoffice.org/t/is-there-any-way-to-copy-calc-cell-content-and-paste-something-else/32173/11 


there is:

oClip =
ctx.getServiceManager().createInstanceWithContext("com.sun.star.datatra
nsfer.clipboard.SystemClipboard", ctx)
oClip.setContents(transferable, None)

where "transferable" is implemented in that example as something that
only supports "text/plain;charset=utf-16" so if you change
copy_with_format to something that follow the model of the above
example implementation of an XTransferable to set a transferable that
provides the data and its mimetype as text/html I would expect it to
work.


Re: paste html using API

2023-03-27 Thread sos

We do it with the functions below

Function ClipboardAsText As String
Dim oClip As Variant, oClipContents As Variant, oTypes As Variant
Dim oConverter, convertedString As String
Dim i As Integer, iPlainLoc As Integer
iPlainLoc = -1
oClip = 
createUnoService("com.sun.star.datatransfer.clipboard.SystemClipboard")

oConverter = createUnoService("com.sun.star.script.Converter")
oClipContents = oClip.getContents()
oTypes = oClipContents.getTransferDataFlavors()
Dim msg As String
msg = "Sorry no usefull text in the ClipBoard found"
For i=LBound(oTypes) To UBound(oTypes)
If oTypes(i).MimeType = "text/plain;charset=utf-16" Then
iPlainLoc = i
Exit For
End If
Next i
If iPlainLoc >= 0 Then
convertedString = 
oConverter.convertToSimpleType(oClipContents.getTransferData(oTypes(iPlainLoc)), 
com.sun.star.uno.TypeClass.STRING)

ClipboardAsText=convertedString
End If
End Function

Sub CopyToClipBoard( sText )
' create SystemClipboard instance
oClip = CreateUnoService( _
"com.sun.star.datatransfer.clipboard.SystemClipboard")
oTR = createUnoListener("Tr_", _
"com.sun.star.datatransfer.XTransferable")
' set data
oClip.setContents(oTR,Null)
sTxtCString = sText
'oClip.flushClipboard() ' does not work
End Sub

Function Tr_getTransferData(aFlavor as com.sun.star.datatransfer.DataFlavor)
' wordt pas aangeroepen als we cntrol+V doen intss is sTxtCString reeds 
gezet

If (aFlavor.MimeType = "text/plain;charset=utf-16") Then
Tr_getTransferData() = sTxtCString
End If
End Function

Function Tr_getTransferDataFlavors()
Dim aFlavor As new com.sun.star.datatransfer.DataFlavor
aFlavor.MimeType = "text/plain;charset=utf-16"
aFlavor.HumanPresentableName = "Unicode-Text"
Tr_getTransferDataFlavors() = array(aFlavor)
End Function

Function Tr_isDataFlavorSupported( _
aFlavor as com.sun.star.datatransfer.DataFlavor) as Boolean
If aFlavor.MimeType = "text/plain;charset=utf-16" Then
Tr_isDataFlavorSupported = true
Else
Tr_isDataFlavorSupported = false
End If
End Function

hope it helps

Fernand

On 26/03/2023 22:26, Zorg wrote:


Thanks

Ok  it seem a goot way

maybe it is obvious but how can i paste the clipboard in my odt 
without using dispatch




Le 26/03/2023 à 21:20, Caolán McNamara a écrit :

On Sun, 2023-03-26 at 12:06 +0200, Zorg wrote:

... We have try using XTransferable but without any success.

I think it should be possible in headless mode to use the clipboard
apis to copy and paste within LibreOffice. But in this case the
clipboard is basically a fake clipboard only for the headless
libreoffice instance, one which doesn't interact with any real system
clipboard.

So for example in your current code I see that copy_with_format copies
into a true X clipboard via xclip so in headless more we can't paste
from there and it has to be non headless to get that to work.

But if the LibreOffice apis to put something into the clipboard are
used then I think it should work to "Paste" within LibreOffice and get
that content back out, even in headless more.

f.e. in this example at
https://ask.libreoffice.org/t/is-there-any-way-to-copy-calc-cell-content-and-paste-something-else/32173/11
there is:

oClip =
ctx.getServiceManager().createInstanceWithContext("com.sun.star.datatra
nsfer.clipboard.SystemClipboard", ctx)
oClip.setContents(transferable, None)

where "transferable" is implemented in that example as something that
only supports "text/plain;charset=utf-16" so if you change
copy_with_format to something that follow the model of the above
example implementation of an XTransferable to set a transferable that
provides the data and its mimetype as text/html I would expect it to
work.



Re: paste html using API

2023-03-26 Thread Zorg

Thanks

Ok  it seem a goot way

maybe it is obvious but how can i paste the clipboard in my odt without 
using dispatch




Le 26/03/2023 à 21:20, Caolán McNamara a écrit :

On Sun, 2023-03-26 at 12:06 +0200, Zorg wrote:

... We have try using XTransferable but without any success.

I think it should be possible in headless mode to use the clipboard
apis to copy and paste within LibreOffice. But in this case the
clipboard is basically a fake clipboard only for the headless
libreoffice instance, one which doesn't interact with any real system
clipboard.

So for example in your current code I see that copy_with_format copies
into a true X clipboard via xclip so in headless more we can't paste
from there and it has to be non headless to get that to work.

But if the LibreOffice apis to put something into the clipboard are
used then I think it should work to "Paste" within LibreOffice and get
that content back out, even in headless more.

f.e. in this example at
https://ask.libreoffice.org/t/is-there-any-way-to-copy-calc-cell-content-and-paste-something-else/32173/11
there is:

oClip =
ctx.getServiceManager().createInstanceWithContext("com.sun.star.datatra
nsfer.clipboard.SystemClipboard", ctx)
oClip.setContents(transferable, None)

where "transferable" is implemented in that example as something that
only supports "text/plain;charset=utf-16" so if you change
copy_with_format to something that follow the model of the above
example implementation of an XTransferable to set a transferable that
provides the data and its mimetype as text/html I would expect it to
work.


Re: paste html using API

2023-03-26 Thread Caolán McNamara
On Sun, 2023-03-26 at 12:06 +0200, Zorg wrote:
> ... We have try using XTransferable but without any success.

I think it should be possible in headless mode to use the clipboard
apis to copy and paste within LibreOffice. But in this case the
clipboard is basically a fake clipboard only for the headless
libreoffice instance, one which doesn't interact with any real system
clipboard.

So for example in your current code I see that copy_with_format copies
into a true X clipboard via xclip so in headless more we can't paste
from there and it has to be non headless to get that to work.

But if the LibreOffice apis to put something into the clipboard are
used then I think it should work to "Paste" within LibreOffice and get
that content back out, even in headless more. 

f.e. in this example at
https://ask.libreoffice.org/t/is-there-any-way-to-copy-calc-cell-content-and-paste-something-else/32173/11
there is:

oClip =
ctx.getServiceManager().createInstanceWithContext("com.sun.star.datatra
nsfer.clipboard.SystemClipboard", ctx)
oClip.setContents(transferable, None)

where "transferable" is implemented in that example as something that
only supports "text/plain;charset=utf-16" so if you change
copy_with_format to something that follow the model of the above
example implementation of an XTransferable to set a transferable that
provides the data and its mimetype as text/html I would expect it to
work.



Re: paste html using API

2023-03-26 Thread Zorg

Hello

Thanks for your help but it won't really help me

it's doesn't matter the language,  I could translate in python.

The main problem is to start from a variable (a html string)

Var1="Lorem Ipsum?Le Lorem Ipsum est 
simplement du faux texte employé dans la composition et la mise en page 
avant impression. Le Lorem Ipsum "


and you want to paste it in a odt and keep the format

If I want to use only headless mode, i must find a solution without 
using the SystemClipboard because the clipboard does not seem to be 
accessible





Le 26/03/2023 à 19:33, Andrew Pitonyak a écrit :

I have no idea if this will be of any use to you or not, but...

I do not generally use the API using Python so I cannot comment on 
using Python.


I have also not used headless mode so I cannot comment on what is and 
is not available other than knowing that dispatches are not available.


Finally, I have done almost nothing using the clipboard, but...

First note that the clipboard should have content of different 
varieties. I assume that you are aware of this because you have a line 
as follows:


 copy_with_format(value, 'text/html')

 And if I look at the clipboard I see this as an available type

   Dim sClipName As String
  Dim oClip As Object
  Dim oFlavors As Variant
  Dim oFlavor As Variant
  Dim i as Integer
  Dim s as String

  sClipName = "com.sun.star.datatransfer.clipboard.SystemClipboard"
  oClip = createUnoService(sClipName)
  oFlavors = oClip.getContents().getTransferDataFlavors()
  s = ""
  For i = LBound(oFlavors) To UBound(oFlavors)
    oFlavor = oFlavors(i)
    s = s & i & " : " & oFlavor.MimeType
    s = s & CHR$(10)
  Next
  MsgBox s


  I see that method defined in this code here:

https://github.com/Probesys/lotemplate/blob/html_formatting/lotemplate/utils.py

  I see that you open a pipe.

  I did some experiments and I am extracting the actual HTML. I assume 
that this is not really what you want, but this is how I extract it 
and display it. I was simply playing around with it out of curiousity.


    Dim sClipName As String
  Dim oClip As Object
  Dim oFlavors As Variant
  Dim oFlavor As Variant
  Dim i as Integer
  Dim idx as Integer
  Dim s as String

  sClipName = "com.sun.star.datatransfer.clipboard.SystemClipboard"
  oClip = createUnoService(sClipName)
  oFlavors = oClip.getContents().getTransferDataFlavors()
  s = ""
  idx = -1
  For i = LBound(oFlavors) To UBound(oFlavors)
    oFlavor = oFlavors(i)
    s = s & i & " : " & oFlavor.MimeType
    If oFlavor.MimeType = "text/html" Then
      idx = i
    End If
    s = s & CHR$(10)
  Next
  'MsgBox s

  If idx < 0 Then
    Print "text/html mime type not found"
    Exit Sub
   End If

  ' Now print the actual HMTL as a string!
  Dim oData As Variant
  oData = oClip.getContents().getTransferData(oFlavors(idx))
  s = ""
  For i = LBound(oData) To UBound(oData)
    s = s & CHR$(oData(i))
  Next
  MsgBox s

If I kept going, then I would attempt to then use the transferable 
content directly, which is obtained from oClip.getContents() I 
believe. But, one of the real questions is Does the clipboard have 
the contents shown in the correct format? I assume it does.


So, perhaps this helps a little anyway.

Andrew Pitonyak


On Sunday, March 26, 2023 06:06 EDT, Zorg  wrote:

Hello

Hope some can help me here

Here is my  problem


We are developing our software using the uno api python

We re trying to to copy a string formatted in html in a libreoffice odt

We manage to do it but using dispatcher
The problem is that dispatcher not working in headless mode so il 
force us to use a xserver (xvfb) et Xclip.


It work but it's far from being optimized.

here is the code 
https://github.com/Probesys/lotemplate/blob/html_formatting/lotemplate/classes.py 
line 349-355


We have try using XTransferable but without any success.

Thanks in advance for your help

Sorg




Re: paste html using API

2023-03-26 Thread Andrew Pitonyak

I have no idea if this will be of any use to you or not, but...

I do not generally use the API using Python so I cannot comment on using 
Python. 

I have also not used headless mode so I cannot comment on what is and is not 
available other than knowing that dispatches are not available. 

Finally, I have done almost nothing using the clipboard, but... 

First note that the clipboard should have content of different varieties. I 
assume that you are aware of this because you have a line as follows: 

 copy_with_format(value, 'text/html')
 
 And if I look at the clipboard I see this as an available type
 
   Dim sClipName As String
  Dim oClip As Object
  Dim oFlavors As Variant
  Dim oFlavor As Variant
  Dim i as Integer
  Dim s as String
  
  sClipName = "com.sun.star.datatransfer.clipboard.SystemClipboard"
  oClip = createUnoService(sClipName)
  oFlavors = oClip.getContents().getTransferDataFlavors()
  s = ""
  For i = LBound(oFlavors) To UBound(oFlavors)
    oFlavor = oFlavors(i)
    s = s & i & " : " & oFlavor.MimeType
    s = s & CHR$(10)
  Next
  MsgBox s
  
  
  I see that method defined in this code here: 
  
  
https://github.com/Probesys/lotemplate/blob/html_formatting/lotemplate/utils.py
  
  I see that you open a pipe. 
  
  I did some experiments and I am extracting the actual HTML. I assume that 
this is not really what you want, but this is how I extract it and display it. 
I was simply playing around with it out of curiousity.
  
    Dim sClipName As String
  Dim oClip As Object
  Dim oFlavors As Variant
  Dim oFlavor As Variant
  Dim i as Integer
  Dim idx as Integer
  Dim s as String
  
  sClipName = "com.sun.star.datatransfer.clipboard.SystemClipboard"
  oClip = createUnoService(sClipName)
  oFlavors = oClip.getContents().getTransferDataFlavors()
  s = ""
  idx = -1
  For i = LBound(oFlavors) To UBound(oFlavors)
    oFlavor = oFlavors(i)
    s = s & i & " : " & oFlavor.MimeType
    If oFlavor.MimeType = "text/html" Then
      idx = i
    End If
    s = s & CHR$(10)
  Next
  'MsgBox s
  
  If idx < 0 Then
    Print "text/html mime type not found"
    Exit Sub
   End If
  
  ' Now print the actual HMTL as a string!
  Dim oData As Variant
  oData = oClip.getContents().getTransferData(oFlavors(idx))
  s = ""
  For i = LBound(oData) To UBound(oData)
    s = s & CHR$(oData(i))
  Next
  MsgBox s

If I kept going, then I would attempt to then use the transferable content 
directly, which is obtained from oClip.getContents() I believe. But, one of the 
real questions is Does the clipboard have the contents shown in the correct 
format? I assume it does. 

So, perhaps this helps a little anyway. 

Andrew Pitonyak


On Sunday, March 26, 2023 06:06 EDT, Zorg  wrote:
  
Hello
Hope some can help me here
Here is my  problem

We are developing our software using the uno api python
We re trying to to copy a string formatted in html in a libreoffice odt
We manage to do it but using dispatcher
The problem is that dispatcher not working in headless mode so il force us to 
use a xserver (xvfb) et Xclip.
It work but it's far from being optimized.
here is the code 
https://github.com/Probesys/lotemplate/blob/html_formatting/lotemplate/classes.py
 line 349-355
We have try using XTransferable but without any success.
Thanks in advance for your help
Sorg   

 


paste html using API

2023-03-26 Thread Zorg

Hello

Hope some can help me here

Here is my  problem


We are developing our software using the uno api python

We re trying to to copy a string formatted in html in a libreoffice odt

We manage to do it but using dispatcher
The problem is that dispatcher not working in headless mode so il force 
us to use a xserver (xvfb) et Xclip.


It work but it's far from being optimized.

here is the code 
https://github.com/Probesys/lotemplate/blob/html_formatting/lotemplate/classes.py 
line 349-355


We have try using XTransferable but without any success.

Thanks in advance for your help

Sorg