diff --git a/README.md b/README.md index 91cc55c..9a9f13a 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ pip install xlsx2csv ``` **positional arguments:** ``` - xlsxfile xlsx file path + xlsxfile xlsx file path, use '-' to read from STDIN outfile output csv file path, or directory if -s 0 is specified ``` **optional arguments:** diff --git a/man/xlsx2csv.1.pod b/man/xlsx2csv.1.pod index 788f3a8..f25d23e 100644 --- a/man/xlsx2csv.1.pod +++ b/man/xlsx2csv.1.pod @@ -50,6 +50,10 @@ xlsx2csv - Convert xlsx xml files to csv format The conversion uses Expat SAX parser for xml processing. +=head1 STDIN + +Use "-" for INFILE to read from the STDIN. + =head1 OPTIONS =over 4 diff --git a/test/run b/test/run index 40b7059..88433a3 100755 --- a/test/run +++ b/test/run @@ -20,15 +20,16 @@ def compare(case, arguments=[]): ext = "xlsx" if os.path.exists("test/%s.xlsm" % case): ext = "xlsm" - + if os.name == 'posix':# in case of Linux - left = subprocess.check_output(["python%s" %pyver, "./xlsx2csv.py"] + arguments + ["test/%s.%s" %(case, ext)]).decode('utf-8').replace('\r','') + command = ["python%s" %pyver] elif os.name == 'nt':# in case of Windows # Use py.exe http://blog.python.org/2011/07/python-launcher-for-windows_11.html on Windows - left = subprocess.check_output(["py", "-%s" %pyver, "./xlsx2csv.py"] + arguments + ["test/%s.%s" %(case, ext)]).decode('utf-8').replace('\r','') + command = ["py", "-%s" %pyver] else: print("os.name is unexpected: "+os.name) sys.exit(1) + left = subprocess.check_output(command + ["./xlsx2csv.py"] + arguments + ["test/%s.%s" %(case, ext)]).decode('utf-8').replace('\r','') f = open("test/%s.csv" %case, "r", encoding="utf-8", newline="") right = f.read().replace('\r','') @@ -41,9 +42,28 @@ def compare(case, arguments=[]): failed = True else: print("OK: %s %s" %(case, pyver)) + + # test STDIN, only works for python3 + if pyver == "2": + continue + + xfile = open("test/%s.%s" %(case,ext), "rb") + stdin = xfile.read() + xfile.close() + + pipe = subprocess.run(command + ["./xlsx2csv.py"] + arguments + ["-"], input = stdin, capture_output = True) + stdinleft = pipe.stdout.decode("utf-8").replace('\r','').replace('\r','') + + if stdinleft != right: + print("FAILED (STDIN): %s %s" %(case, pyver)) + failed = True + else: + print("OK (STDIN): %s %s" %(case, pyver)) + if failed: sys.exit(1) + compare("datetime", ["--dateformat=%Y-%m-%d %H:%M:%S"]) compare("empty_row") compare("junk-small") diff --git a/xlsx2csv.py b/xlsx2csv.py index b76aaca..ce6e80b 100755 --- a/xlsx2csv.py +++ b/xlsx2csv.py @@ -1067,7 +1067,7 @@ def main(): if "ArgumentParser" in globals(): parser = ArgumentParser(description="xlsx to csv converter") - parser.add_argument('infile', metavar='xlsxfile', help="xlsx file path") + parser.add_argument('infile', metavar='xlsxfile', help="xlsx file path, use '-' to read from STDIN") parser.add_argument('outfile', metavar='outfile', nargs='?', help="output csv file path") parser.add_argument('-v', '--version', action='version', version=__version__) nargs_plus = "+" @@ -1216,7 +1216,7 @@ def main(): try: if os.path.isdir(options.infile): convert_recursive(options.infile, sheetid, outfile, kwargs) - elif not os.path.exists(options.infile): + elif not os.path.exists(options.infile) and options.infile != "-": raise InvalidXlsxFileException("Input file not found!") else: xlsx2csv = Xlsx2csv(options.infile, **kwargs)