Skip to content

Instantly share code, notes, and snippets.

@dannguyen
Last active August 3, 2018 20:51
Show Gist options
  • Save dannguyen/d83f27a93b2e6f80edda22cfa0f0a1d6 to your computer and use it in GitHub Desktop.
Save dannguyen/d83f27a93b2e6f80edda22cfa0f0a1d6 to your computer and use it in GitHub Desktop.

Revisions

  1. dannguyen revised this gist Apr 12, 2016. 2 changed files with 28 additions and 0 deletions.
    28 changes: 28 additions & 0 deletions xlrd-xls-to-csv--treer.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    """
    Same as the other script, but attempts to preserve the same folder
    structure in `csv` as it exists in `xlsx`
    """

    from csv import writer
    from glob import glob
    from os.path import basename, dirname, join
    from os import makedirs
    from xlrd import open_workbook
    import re

    CSV_DIRNAME = 'csv'
    XLS_DIRNAME = 'xlsx'

    for xfname in glob(join(XLS_DIRNAME, '*', '*.xlsx')):
    cdir = re.sub(r'^' + XLS_DIRNAME, CSV_DIRNAME, dirname(xfname))
    makedirs(cdir, exist_ok=True)
    cname = join(cdir, basename(xfname) + '.csv')
    print("writing to", cname)
    cf = open(cname, 'w')
    cv = writer(cf)
    book = open_workbook(xfname)
    sheet = book.sheets()[0]
    for n in range(sheet.nrows):
    rowvals = sheet.row_values(n)
    cv.writerow(rowvals)
    cf.close()
    File renamed without changes.
  2. dannguyen revised this gist Apr 12, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion xlrd-xls-to-csv.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    """
    This code processes each xlsx file in the xlsx/* path,
    then writes the data (a list of values) to CSV files stored in the csv/ path
    then writes the data (a list of values) into a single CSV folder (i.e. flat structure)
    """

    from csv import writer
  3. dannguyen created this gist Apr 12, 2016.
    26 changes: 26 additions & 0 deletions xlrd-xls-to-csv.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    """
    This code processes each xlsx file in the xlsx/* path,
    then writes the data (a list of values) to CSV files stored in the csv/ path
    """

    from csv import writer
    from glob import glob
    from os.path import basename, join
    from os import makedirs
    from xlrd import open_workbook
    import re

    CSV_DIRNAME = 'csv'
    makedirs(CSV_DIRNAME, exist_ok=True)

    for xfname in glob(join('xlsx', '*', '*.xlsx')):
    cname = join(CSV_DIRNAME, basename(xfname) + '.csv')
    print("writing to", cname)
    cf = open(cname, 'w')
    cv = writer(cf)
    book = open_workbook(xfname)
    sheet = book.sheets()[0]
    for n in range(sheet.nrows):
    rowvals = sheet.row_values(n)
    cv.writerow(rowvals)
    cf.close()