Ask Your Question
0

What Python code opens a specific Calc file?

asked 2018-07-08 05:14:01 +0200

cellsee6 gravatar image

Running Libreoffice 6.0.4.2 on Windows 7 Pro

I have searched all over for an answer as to how to open an existing Calc file using Python. I have read Christopher Bourez’s blog . He shows how to launch Calc so it opens on the desktop. Using his code I can launch the Python shell and connect to LibreOffice. He does not show how to open a specific Calc file.

I want to export 50 Calc files to csv files . Doing this manually takes a long time.

I am trying to understand how to open a specific Calc file . All my Calc files are in the C:\Document folder.

My question: Once Python is connected to Libreoffice , what commands are needed to open a specific Calc file stored in C:\document folder?

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
0

answered 2018-07-09 07:34:03 +0200

Xoristzatziki gravatar image
ctx = uno.getComponentContext()
asmgr = ctx.ServiceManager
thepathtodoc = "C:\myfile.ods" #"/home/foo/Documents/file.odt"
thedocurl = uno.systemPathToFileUrl(thepathtodoc)
adesktop = asmgr.createInstanceWithContext("com.sun.star.frame.Desktop", ctx)
somefile = adesktop.loadComponentFromURL(thedocurl,"_blank",0,array())
edit flag offensive delete link more
0

answered 2018-07-08 13:52:01 +0200

hjek gravatar image

updated 2018-07-08 13:57:30 +0200

I want to export 50 Calc files to csv files . Doing this manually takes a long time.

With Unoconv you can do that on a POSIX shell like this:

unoconv -f csv the-folder-with-the-50-calc-files/*.ods

(I'm not familiar enough with Windows shell to know if that works, but from what I can read, it seams to support globbing, e.g. the *. Otherwise Busybox for Windows inculdes a POSIX shell. Someone who knows Windows may have more of a clue here.)

Using his code I can launch the Python shell and connect to LibreOffice. He does not show how to open a specific Calc file.

If it for some reason is a requirement for you to use Python, give these PyUNO examples a look. It looks like it would be fairly simple to add CSV export to the ooextract.py example which extracts plaintext, HTML or PDF.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2018-07-08 05:14:01 +0200

Seen: 98 times

Last updated: Jul 09