Created
April 1, 2019 23:21
-
-
Save Uberi/cc0c900a74884e8c9c52141912d1951d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import shutil, subprocess, tempfile, os | |
import openpyxl | |
def load_workbook_xls(filename, **kwargs): | |
"""Same as openpyxl.load_workbook, but supports XLS files instead of XLSX files.""" | |
soffice_path = shutil.which("soffice") | |
if soffice_path is None: | |
raise EnvironmentError("Can't find `soffice` executable - ensure Libreoffice Calc is installed correctly") | |
with tempfile.TemporaryDirectory() as temp_xlsx_directory: | |
subprocess.check_call([ | |
soffice_path, '--headless', '--convert-to', 'xlsx:Calc MS Excel 2007 XML', "--outdir", temp_xlsx_directory, os.path.realpath(filename) | |
], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, cwd=temp_xlsx_directory) | |
xlsx_file = os.listdir(temp_xlsx_directory)[0] | |
return openpyxl.load_workbook(os.path.join(temp_xlsx_directory, xlsx_file), **kwargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment