1

I have to convert .las files in one directory to .xlsx files using las2excelbulk function.

Currently I can do that in command prompt but I want to do it using Python: is it possible?

Here's the link which i referred https://lasio.readthedocs.io/en/latest/exporting.html

  • Open CMD

  • switch to the folder having las files using " cd

  • las2excelbulk -r -i

The file would be converted.

# this is working for only one file

import lasio

las = lasio.read('*.las')

las.to_excel('testsamplelas.xlsx')
DC Slagel
  • 528
  • 1
  • 7
  • 13
  • Command-line arguments are passed to scripts as a list of strings in `sys.argv`, so your script can look there for arguments. You can determine the current-working-directory by calling `os.getcwd()`. – martineau Jan 15 '19 at 11:02

1 Answers1

2

The argument to lasio.read() can only be a single filename, but you can use the os and fnmatch modules in the Python standard library to step through all .las files recursively.

import fnmatch
import os
import lasio

for root, dirnames, filenames in os.walk("your_directory"):
    for filename in fnmatch.filter(filenames, '*.las'):
        path = os.path.join(root, filename)
        las = lasio.read(path, ignore_header_errors=True)
        las.to_excel(path + ".xlsx")

lasio.read(..., ignore_header_errors=True) is the equivalent of las2excelbulk -i.

It may also be useful to inspect the code behind the las2excelbulk command line tool for more information.

kii
  • 497
  • 3
  • 11
  • I tested your solution but keep coming up with the following error: "FileNotFoundError: [Errno 2] No such file or directory: 'P-129_out.LAS'" however when I test the .las with lasio, and simply read to do a single file conversion to a csv, there are no problems. – NightLearner Sep 29 '19 at 22:42
  • Hi @Lgeo - I think there was an error in my answer - see revised code above. I added `path = os.path.join(root, filename)` and used `path` instead of `filename` subsequently. – kii Sep 30 '19 at 06:45
  • it worked great thanks! Also I switched excel files for csvs because that was the other file type I need and at least that way I don't have to deal with the sheets issue. – NightLearner Oct 01 '19 at 00:42