| 
									
										
										
										
											2015-12-21 23:57:18 -08:00
										 |  |  | #!/usr/bin/python3 | 
					
						
							| 
									
										
										
										
											2013-01-04 14:38:49 -06:00
										 |  |  | # ods2xls.py | 
					
						
							| 
									
										
										
										
											2013-01-03 18:57:19 -06:00
										 |  |  | # adapted from ssconv.py | 
					
						
							|  |  |  | # see also | 
					
						
							|  |  |  | #  https://help.libreoffice.org/Common/About_Converting_Microsoft_Office_Documents | 
					
						
							|  |  |  | #  http://wiki.openoffice.org/wiki/Documentation/DevGuide/Spreadsheets/Filter_Options | 
					
						
							|  |  |  | #  http://linuxsleuthing.blogspot.com/2012/01/unoconv-is-number-one.html | 
					
						
							| 
									
										
										
										
											2014-09-04 12:09:00 -04:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											2015-12-21 23:57:18 -08:00
										 |  |  | # Copyright (C) 2013, Tom Marble. | 
					
						
							| 
									
										
										
										
											2014-09-04 12:09:00 -04:00
										 |  |  | # | 
					
						
							|  |  |  | # This software's license gives you freedom; you can copy, convey, | 
					
						
							|  |  |  | # propogate, redistribute and/or modify this program under the terms of | 
					
						
							|  |  |  | # the GNU  Lesser General Public License (LGPL) as published by the Free | 
					
						
							|  |  |  | # Software Foundation (FSF), either version 2.1 of the License, or (at your | 
					
						
							|  |  |  | # option) any later version of the LGPL published by the FSF. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # This program is distributed in the hope that it will be useful, but | 
					
						
							|  |  |  | # WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
					
						
							|  |  |  | # General Public License for more details. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # You should have received a copy of the GNU Lesser General Public License | 
					
						
							|  |  |  | # along with this program in a file in the toplevel directory called | 
					
						
							|  |  |  | # "LGPLv2.1".  If not, see <http://www.gnu.org/licenses/>. | 
					
						
							| 
									
										
										
										
											2013-01-03 18:57:19 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | import os | 
					
						
							|  |  |  | import ooutils | 
					
						
							| 
									
										
										
										
											2013-01-04 14:38:49 -06:00
										 |  |  | import time | 
					
						
							| 
									
										
										
										
											2013-01-03 18:57:19 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | import uno | 
					
						
							|  |  |  | from com.sun.star.task import ErrorCodeIOException | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class SSConverter: | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     Spreadsheet converter class. | 
					
						
							| 
									
										
										
										
											2013-01-04 14:38:49 -06:00
										 |  |  |     Converts spreadsheets to XLS files. | 
					
						
							| 
									
										
										
										
											2013-01-03 18:57:19 -06:00
										 |  |  |     """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __init__(self, oorunner=None): | 
					
						
							|  |  |  |         self.desktop  = None | 
					
						
							|  |  |  |         self.oorunner = None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def convert(self, inputFile, outputFile): | 
					
						
							|  |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2013-01-04 14:38:49 -06:00
										 |  |  |         Convert the input file (a spreadsheet) to a XLS file. | 
					
						
							| 
									
										
										
										
											2013-01-03 18:57:19 -06:00
										 |  |  |         """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Start openoffice if needed. | 
					
						
							|  |  |  |         if not self.desktop: | 
					
						
							|  |  |  |             if not self.oorunner: | 
					
						
							|  |  |  |                 self.oorunner = ooutils.OORunner() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-04 14:38:49 -06:00
										 |  |  |             # DEBUG | 
					
						
							|  |  |  |             print('oorunner should be working here') | 
					
						
							| 
									
										
										
										
											2013-01-03 18:57:19 -06:00
										 |  |  |             self.desktop = self.oorunner.connect() | 
					
						
							| 
									
										
										
										
											2013-01-04 14:38:49 -06:00
										 |  |  |             time.sleep(1) | 
					
						
							|  |  |  |             print(os.popen('fuser -u 8100/tcp').read()) | 
					
						
							|  |  |  |             print('connected to LibreOffice...') | 
					
						
							| 
									
										
										
										
											2013-01-03 18:57:19 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |         inputUrl  = uno.systemPathToFileUrl(os.path.abspath(inputFile)) | 
					
						
							|  |  |  |         outputUrl = uno.systemPathToFileUrl(os.path.abspath(outputFile)) | 
					
						
							|  |  |  |         document  = self.desktop.loadComponentFromURL(inputUrl, "_blank", 0, ooutils.oo_properties(Hidden=True)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             # Additional property option: | 
					
						
							|  |  |  |             #   FilterOptions="59,34,0,1" | 
					
						
							|  |  |  |             #     59 - Field separator (semicolon), this is the ascii value. | 
					
						
							|  |  |  |             #     34 - Text delimiter (double quote), this is the ascii value. | 
					
						
							|  |  |  |             #      0 - Character set (system). | 
					
						
							|  |  |  |             #      1 - First line number to export. | 
					
						
							|  |  |  |             # | 
					
						
							|  |  |  |             # For more information see: | 
					
						
							|  |  |  |             #   http://wiki.services.openoffice.org/wiki/Documentation/DevGuide/Spreadsheets/Filter_Options | 
					
						
							|  |  |  |             # | 
					
						
							| 
									
										
										
										
											2013-01-04 14:38:49 -06:00
										 |  |  |             document.storeToURL(outputUrl, ooutils.oo_properties(FilterName="MS Excel 97")) | 
					
						
							| 
									
										
										
										
											2013-01-03 18:57:19 -06:00
										 |  |  |         finally: | 
					
						
							|  |  |  |             document.close(True) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if __name__ == "__main__": | 
					
						
							|  |  |  |     from sys import argv | 
					
						
							|  |  |  |     from os.path import isfile | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if len(argv) == 2  and  argv[1] == '--shutdown': | 
					
						
							|  |  |  |         ooutils.oo_shutdown_if_running() | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         if len(argv) < 2: | 
					
						
							| 
									
										
										
										
											2015-12-21 23:57:18 -08:00
										 |  |  |             print("USAGE:") | 
					
						
							|  |  |  |             print("  python %s INPUT-FILE [INPUT-FILE ...]" % argv[0]) | 
					
						
							|  |  |  |             print("OR") | 
					
						
							|  |  |  |             print("  python %s --shutdown" % argv[0]) | 
					
						
							| 
									
										
										
										
											2013-01-03 18:57:19 -06:00
										 |  |  |             exit(255) | 
					
						
							|  |  |  |         if not isfile(argv[1]): | 
					
						
							| 
									
										
										
										
											2015-12-21 23:57:18 -08:00
										 |  |  |             print("File not found: %s" % argv[1]) | 
					
						
							| 
									
										
										
										
											2013-01-03 18:57:19 -06:00
										 |  |  |             exit(1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             i = 1 | 
					
						
							|  |  |  |             converter = SSConverter() | 
					
						
							|  |  |  |             while i < len(argv): | 
					
						
							|  |  |  |                 odsname = argv[i] | 
					
						
							| 
									
										
										
										
											2013-01-04 14:38:49 -06:00
										 |  |  |                 xlsname = odsname.replace('.ods', '.xls') | 
					
						
							| 
									
										
										
										
											2015-12-21 23:57:18 -08:00
										 |  |  |                 print('%s => %s' % (odsname, xlsname)) | 
					
						
							| 
									
										
										
										
											2013-01-04 14:38:49 -06:00
										 |  |  |                 converter.convert(odsname, xlsname) | 
					
						
							| 
									
										
										
										
											2013-01-03 18:57:19 -06:00
										 |  |  |                 i += 1 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-21 23:57:18 -08:00
										 |  |  |         except ErrorCodeIOException as exception: | 
					
						
							|  |  |  |             print("ERROR! ErrorCodeIOException %d" % exception.ErrCode) | 
					
						
							| 
									
										
										
										
											2013-01-03 18:57:19 -06:00
										 |  |  |             exit(1) |