Skip to content

Instantly share code, notes, and snippets.

@ckhung
Last active December 3, 2021 02:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ckhung/61a24c96277e53a275015d513634687d to your computer and use it in GitHub Desktop.
Save ckhung/61a24c96277e53a275015d513634687d to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
# https://github.com/pyexcel/pyexcel-ods/issues/41
# pyexcel-ods issue: use "<text:p>" instead of "<text:line-break/>" for newlines (line breaks) in cells
import argparse, re, sys, json
from pyexcel_ods3 import save_data
from warnings import warn
parser = argparse.ArgumentParser(
description='read many text files into two columns of an ods file, with filenames as the key column and contents as the value column',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('-o', '--outfn', type=str, default='out.ods',
help='output file name')
parser.add_argument('-s', '--sheet', type=str, default='main',
help='sheet name')
parser.add_argument('-x', '--keepext', type=bool, default=False,
help='keep extension?')
parser.add_argument('textfile', nargs='*', help='input files')
args = parser.parse_args()
sheet = []
for fn in args.textfile:
with open(fn) as F:
val = F.read()
fn = re.search(r'([^/]+$)', fn).group(1)
if not args.keepext:
fn = re.sub(r'\.\w+$', '', fn)
sheet.append([fn, val])
save_data(args.outfn, { args.sheet: sheet })
#args.keycol
#print(json.dumps(sheet, ensure_ascii=False))
spine
vitamin C
antioxidants
temperate
fiber
summer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment