Is there a possibility to close a pyqt dialog in libre office properly (python macro)? - libreoffice

I am trying to write a python macro for libre office 7.4 on windows 10 that creates a dialog with pyqt6.
def open_qt_dialog():
import sys
from PyQt6.QtWidgets import (
QApplication,
QWidget,
)
app = QApplication([])
window = QWidget()
window.setWindowTitle("Simple Dialog")
window.show()
sys.exit(app.exec())
Regardless of whether I use a QDialog or a QMainWindow, I get the following error when I press the "X" button in the title bar. After that the dialog is closed.
com.sun.star.uno.RuntimeException: <class 'SystemExit'>: 0, traceback follows
File "C:\Program Files\LibreOffice\program\pythonscript.py", line 915, in invoke
ret = self.func( *args )
File "C:\Users\<username>\AppData\Roaming\LibreOffice\4\user\Scripts\python\Test.py", line 101, in open_qt_dialog
sys.exit(app.exec())
If I then run the macro again, the dialog is called again and shortly after that libre office crashes. This behavior occurs regardless of whether the macro is executed via the menu or via a button.
Only closing the dialog seems to be a problem. Interaction with libre office (e.g. writing a value into a cell, when pressing a button) works so far.
I already tried to overwrite the closeEvent method using the quit method on the mainwindow. But it just closed (or crashed) the whole libre office application. Do i mess anything?
Do I still have to adjust the sys.exit command in any way? I have the suspicion that sys.exit tries to close libre office completely and not the dialog itsself.
If I run the script via pycharm (connected to uno via uno bridge), then I can close the dialog without any problems.

Related

Can someone confirm the following bug in Office.js on macOS (Microsoft Word)?

The following very simple code works fine on Word Online and Word 2016 on Windows 10.
It works the first time it is used on macOS and Word 2016, but then throws an error on any subsequent use...until the page is refreshed and then it works again.
To reproduce:
Open a dialog window using the dialogAPI
Click to close the new window
Click on the button that triggers the opening of the dialog window
On the second attempt no window opens on Mac.
I am on Office Insider Slow running Word for Mac Version 116.13.1 (180523). I have had this issue since a previous release of Office Insider Slow.
The error thrown is:
TypeError: undefined is not an object (evaluating 'dialog.addEventHandler')
It seems that, on a Mac, the dialog object is not closed when the dialog window is closed.
Code:
dialogUrl = dialogUrl + 'qLTI.html';
Office.context.ui.displayDialogAsync(dialogUrl, {height: 90, width: 70},
function (asyncResult) {
dialog = asyncResult.value;
dialog.addEventHandler(Office.EventType.DialogMessageReceived, processMessage);
}
);
Trying to get confirmation and also flag MSFT. Thank you.
This was a known issue to MSFT and was fixed in a June release.
See https://github.com/OfficeDev/office-js/issues/197.

Why does a right click sent from pywinauto not change the state of the grayed out connect button in informatica, but a real mouse button click does?

I am trying to write a script using pywinauto to open a repository in Informatica powercenter workflow manager (v9.6.1)
After the below lines are executed, I get the context menu with the connect option as in the screenshot. Also notice the 'connect' button on the toolbar stays grayed out. If I use the actual mouse and click on the treeview item for the repository I want to connect to, the connect button turns green and enabled. But not when pywinauto sends the right click.
from pywinauto.application import Application
from pywinauto.keyboard import SendKeys
import time
app = Application().Start(cmd_line=r'C:\PowerCenterClient\client\bin\pmwflmgr.exe')
informaticaworkflowmanager = app[u'Informatica::WorkflowManager']
informaticaworkflowmanager.Wait('ready')
time.sleep(2)
systreeview = informaticaworkflowmanager[u'TreeView']
tree_item = systreeview.GetItem([u'Repositories', u'REP'])
tree_item.Select()
tree_item.Click(button='right',double=True,where='icon')
I tried using SendKeys to send 'c' followed by {ENTER} and it just returns without doing anything. Also if I use the actual keyboard on the right click context menu generated after the above pywinauto steps run- it selects the connect menu when i press c , but it just doesn't do anything when I press Enter. I think somehow the connect function is not enabled.
I am clueless if any other input is required by the application to enable it. I also tried using the informaticaworkflowmanager.MenuItem(u'&Repository->&Connect...') option but to no avail- it says it is disabled.
Any pointers to help get pywinauto to open a repository are welcome!
Just summarizing the comments...
First try running the script as Administrator. If it doesn't help, use method click_input(). It runs real click like a user does. Method click() sends WM_CLICK or BM_CLICK which is probably not handled by the app in grayed button state.
If you run the script as Administrator, it should inherit privileges for child process by default. There is no special flag in method start() because some functions may not work if target app process has higher privileges. Anyway elevation usually requires confirmation from user and this Security Confirmation dialog can't be automated by OS design (even click_input() with hard coded coordinates won't work while this dialog was shown, I checked it a while ago).

Enthought Canopy iPython console fills with text and loses prompt

I'm running the latest academic distribution of the Canopy IDE on a Windows 7 machine. In the iPython console, if I ask for a docstring (for example, typing "linspace?"), the resulting text fills up the console and gets cut off at the top and bottom. Afterwards, there is no longer an "In [#]" prompt and I cannot type any further commands into the console. If I don't want to lose all of the variables I have defined during the session, I have found that I can open a new editor window (File/New Editor Window) and I will once again have an "In [last #]" prompt where I can enter commands. However, this is a rather a nuisance. I would like to be able to view docstrings without having to open a new window each time. Is there a way I can fix this?
For reference, I have Canopy version 1.0.1.1189.
Update: this only seems to happen when I use the question mark, not if I enter, for example, "help(linspace)." Maybe I should just stick to using help() instead of question marks?
This isn't specific to Canopy and should apply to IPython in general. When you print a long docstring (i.e. one longer than your screen), it prints the output to something call a "pager" which allows you to scroll through the text. I think IPython (more or less) uses less.
When you're in the pager, you can press the q key to exit (and return to the IPython prompt). (Also, space bar pages down, b pages up.)

How to auto execute a macro when opening a Powerpoint presentation?

I have a pretty basic question, but could not find the answer on internet.In Powerpoint 2010, I have a macro that I would like to be executed everytime the Powerpoint document is opened. How to achieve this ?Thanks !
While Auto_Open doesn't run in a PowerPoint presentation, you can fake it. Add a CustomUI part to the presentation, then use the CustomUI OnLoad callback to run code when the presentation opens. The CustomUI part needs no more than just the CustomUI tags.
Get the Custom UI Editor from here: http://openxmldeveloper.org/articles/customuieditor.aspx
Open the presentation in the Custom UI Editor. Insert a CustomUI part from the Insert menu:
Now enter some simple RibbonX code, like this:
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui"
onLoad="MyOnloadProcedure" >
</customUI>
Now write your on-open procedure:
Sub MyOnloadProcedure()
MsgBox "Hello"
End Sub
If you have both this and the Auto_Open procedure in an add-in, Auto_Open runs first.
Full disclosure: while I thought of using this approach and have used it in Excel, I waited until I first encountered it on the PPT Alchemy web site: Run Code When PowerPoint Opens.
I am using PowerPoint 2016 so I can't speak for earlier versions although the documentation indicates this works for PowerPoint versions 2016, 2013, 2010, 2007. This method DOES NOT require any add-ins or application hooks to handle events.
Reference link:
https://support.office.com/en-us/article/command-line-switches-for-microsoft-office-products-079164cd-4ef5-4178-b235-441737deb3a6?ocmsassetID=HA010153889&CTT=1&CorrelationId=ea39d200-aa81-4d6e-8302-afff4c65859e&ui=en-US&rs=en-US&ad=US#ID0EAABAAA=PowerPoint,_PowerPoint_Viewer)
Start PowerPoint from command line and use the /M switch to have PowerPoint run a specified macro when it starts a named presentation file.
The easiest way to do this is to create a shortcut to the PowerPoint application. Then go to the Properties window for the shortcut and select the Shortcut tab. Next, add the /M switch, your presentation file name (including path), and the name of the macro to run (case sensitive and must be part of the presentation) to the end of the Target field. Double-click the shortcut and voila!
Example:
My presentation is C:\myPPTpres.pptm and the macro is Run_Slide_Show so I will need to add
/M "C:\myPPTpres.pptm" "Run_Slide_Show" to the end of the existing text in the Target field.
"C:\Program Files (x86)\Microsoft Office\root\Office16\POWERPNT.EXE"
/M "C:\myPPTpres.pptm" "Run_Slide_Show"
Make sure you are running a macro-enabled version of the presentation (in 2016 it has the .pptm extension.)
Important note if you are trying to start a slide show automatically using the macro. Add a delay of one or more seconds at the very beginning of your macro to allow the application to finish its startup sequence. If you do not the slideshow will start but the application will steal focus as it completes its startup, pushing your slideshow to the background.
Assuming the type of macro to be Auto Events related such as Auto_open(), Auto_close(), Auto_print() etc., for powerpoint to execute macros on opening powerpoint file we require Powerpoint Add-Ins to be included or other option could be to save the file as .ppa or .ppam.
For more details and to download the AutoEvents zip file refer the link: http://skp.mvps.org/autoevents.htm

MATLAB - How do I automatically close the Import Wizard (command window)?

I am invoking the "uiimport" command within a program, opening the Import Wizard to display some data. I would like to be able to automatically close the Wizard within a function (equivalent to "close(gcf)" for a figure), as opposed to having to click "Finish" or "Cancel" or whatever. Is there a way to do this, or should I just find a different way to display the data?
TIA for your help.
I would find a better way to show the data. uiimport is modal, so execution is suspended until it closes, so the script that started it cannot close it.