I have searched for an answer, but nothing has helped so far. I have a method that I want to use to create an odt file and fill it with text. I also want the user to view the file when it is created. I am using python 3.4.3 unotools 0.3.3 LinuxMint 17.1 LibreOffice 4.2.8.2
The issue:
unotools.errors.ConnectionError: failed to connect: ('socket,host=localhost,port=8100', {})
The unotools sample worked fine from terminal - created and saved a sample.odt without errors. My draft code:
def writer_report(self):
subprocess.Popen(["soffice", "--accept='socket,host=localhost,port=8100;urp;StarOffice.Service'"])
time.sleep(5) # using this to give time for LibreOffice to open - temporary
context = connect(Socket('localhost', '8100'))
writer = Writer(context)
writer.set_string_to_end('world\n')
writer.set_string_to_start('hello\n')
writer.store_to_url('output.odt','FilterName','writer8')
writer.close(True)
The LibreOffice application opens and remains open. However, the connection seem to be lost.I hope someone can give me assistance, thank you.
I do not recommend code like this:
subprocess.Popen(...)
time.sleep(...)
It is better to use a shell script to start soffice and then call the python script.
However if you are determined to run soffice in a subprocess, then I recommend increasing the sleep time to at least 15 seconds.
See https://forum.openoffice.org/en/forum/viewtopic.php?t=1014.
Thanks for the advice. I did want this run a a subprocess. I tried extending the time but still no joy.I am now looking at using the Python odfpy 1.3.3 package which after beginning to use for a day or two, I already am having more success with.
Related
I made a simple project in python that pings a server every few seconds and I want to store the ping data in a .txt file. (It might also be cool to put it in a GUI but I need it in a txt file for now). Also, it just shows the ping in the terminal so I have no idea how I would make it go into a txt because I'm new at coding.
(here's my code btw)
import os
import time
while 1:
os.system('ping 1.1.1.1 -n 1')
time.sleep(5)
I didn't try much because I couldn't figure out anything I looked up stuff and nothing was what I wanted.
(also I'm a noob at coding anyways)
You'll have to run your code with Popen instead of os.system (which is a bad idea in most cases, anyway, for security reasons).
With Popen (python.org -> documentation is your friend!) you can capture the output of the programs you run. You can then write that to a file object. (That's a built-in type in python. Again, official documentation on this is good and comes with examples!)
I honestly don't see a reason to write the results of ping to a file. Wouldn't you just care about whether that ping worked and was reasonably fast? Maybe extract that information instead and just log it instead!
I use Python 2.7/PySerial scripts to run tests on devices with an embedded Linux. Due to a recent software change, the Linux box generates a number of log files in .csv format. I need to fetch them. I can't enable any server features in the Linux; I only have a serial connection.
I can of course read the file content out and capture it as text, but this is clumsy and unreliable - I would rather copy the files. Two days of search, and I'm still clueless (Generic problem with me!).
Any hints, please? Please be gentle - this is my first question... :)
Once you get a serial terminal you can use sz (part of lrzsz) to send the files via ZModem. Simply use a serial comm program on the other side (Hyperterminal?) that understands ZModem and the files can be transferred over.
I thank you very much for the proposed solutions. Unfortunately, neither work (I can not enable anything extra on the Linux box), and they are both outside the desired Python environment.
I think it's a kludge, but i'll have to ask for a
cat logfile
as a text string, and attempt to catch the prompt at the end.
Thank you for your time and effort.
We have been running a script on partner's computer for 18 hours. We underestimated how long it would take, and now need to turn in the results. Is it possible to stop the script from running, but still have access to all the lists we are building?
We need to add additional code to the one we are currently running that will use the lists being populated right now. Is there a way to stop the process, but still use (what has been generated of) the lists in the next portion of code?
My partner was using python interactively.
update
We were able to successfully print the results and copy and paste after interrupting the program with control-C.
Well, OP doesn't seem to need an answer anymore. But I'll answer anyway for anyone else coming accross this.
While it is true that stopping the program will delete all data from memory you can still save it. You can inject a debug session and save whatever you need before you kill the process.
Both PyCharm and PyDev support attaching their debugger to a running python application.
See here for an explanation how it works in PyCharm.
Once you've attached the debugger, you can set a breakpoint in your code and the program will stop when it hits that line the next time. Then you can inspect all variables and run some code via the 'Evaluate' feature. This code may save whatever variable you need.
I've tested this with PyCharm 2018.1.1 Community Edition and Python 3.6.4.
In order to do so I ran this code which I saved as test.py
import collections
import time
data = collections.deque(maxlen=100)
i = 0
while True:
data.append(i % 1000)
i += 1
time.sleep(0.001)
via the command python3 test.py from an external Windows PowerShell instance.
Then I've opened that file in PyCharm and attached the debugger. I set a Breakpoint at the line i += 1 and it halted right there. Then I evaluated the following code fragment:
import json
with open('data.json', 'w') as ofile:
json.dump(list(data), ofile)
And found all entries from data in the json file data.json.
Follow-up:
This even works in an interactive session! I ran the very same code in a jupyter notebook cell and then attached the debugger to the kernel. Still having test.py open, I set the breakpoint again on the same line as before and the kernel halted. Then I could see all variables from the interactive notebook session.
I don't think so. Stopping the program should also release all of the memory it was using.
edit: See Swenzel's comment for one way of doing it.
I have this really strange problem, basically I want to start xpdf (or Libreoffice) from my Python script, that is started by a systemd-service. When I start the script from terminal everything is working fine, but when I plug in my USB device that start the Service, I'll get this Error in my syslog:
sh[2321]: Error: Can't open Display
This error has something to do with X11, that's what my Google searches tell me.
So, my question is: How can I properly run a program like xpdf or libreoffice from Python?
import subprocess
subprocess.call("/usr/bin/xpdf")
This is it, basically. I know that it has something to do with the graphical enviroment, but I don't know how I can solve it.
The X display system has very good security to stop random local processes from just displaying stuff to the local screen (It was more a problem in the old days of expensive Sun and SGI systems where computer labs would often let users telnet to other boxes. Much fun could be had!).
If the user running the xpdf is the same user as the one who's logged into the X session, then you simply need to tell xpdf where to connect it's UI to. This is usually done by exporting DISPLAY=:0 to the environment, which means "connect to the first local screen". Most X programs also support -display :0 argument.
So do:
/usr/bin/xpdf -display :0
or:
DISPLAY=:0 /usr/bin/xpdf
It's very unlikely that you have more than one X session so :0 will work 99% of the time.
Since the issue is that xpdf isn't finding a display to connect to, we have two basic options: find and authenticate with an existing display, or make a new one. The latter is usually easier, something like:
xinit /usr/bin/xpdf -fullscreen $PDFFILE -- :2
This would start a new X display :2 running only xpdf, not even a window manager.
It finally worked, after trying and going crazy for around 2 weeks.
What worked was
os.system("DISPLAY=:0 /usr/bin/xpdf)
I know that subprocess.call is the better way to call the program, but it doesn't seem to work right now.
I'll try the way that Yann suggested later on, but for now I'm just overwhelmed with joy that it just works.
Thank you all for your help, I really appreciate it!
At the begining i would like to state that i did look for an answer before posting my question, but if i missed anything I'm really sorry.
Ok to the point.
I'm trying to create a tool that will monitor behaviour of my 2 external devices comunicating over BT(communication over BT i have pretty much solved). but what i'm strugling with is monitoring them.
So Manually i open cmdline 2 times and from there i use putty to connect to devices and do stuff.
Now I want(and pretty much need) to do the manual part in python. So i tried using subprocess.Popen to connect to cmdline(and from there to putty) but the problem is that this only works as request/response. what i need is to open (and keep) cmdline streamlike connection and pass and receive commands/response without closing.
P.S. I'm using windows enviroment and python 2.7.
Thank You for any response.
Kind Regards.