Skip to content

input: add "-" notation for reading from stdin#264

Merged
dilshod merged 1 commit intodilshod:masterfrom
ferdinandyb:master
Jul 31, 2023
Merged

input: add "-" notation for reading from stdin#264
dilshod merged 1 commit intodilshod:masterfrom
ferdinandyb:master

Conversation

@ferdinandyb
Copy link
Copy Markdown
Contributor

Currently, the only way to read from stdin is using a workaround, by running xlsx2csv /dev/stdin < example.xlsx. Add support for the standard notation of using "-" where a file argument would be expected to read from stdin. This allows for writing xlsx2csv - < example.xslx but more importantly in | xlsx2csv -.

This commit only adds support for python3 and will fall back to the previous behaviour for python2. For compatibility reasons it does not use type=argparse.FileType('r'), but parses "-" manually.

Fixes: #263

Currently, the only way to read from stdin is using a workaround, by
running `xlsx2csv /dev/stdin < example.xlsx`. Add support for the
standard notation of using "-" where a file argument would be expected
to read from stdin. This allows for writing `xlsx2csv - < example.xslx`
but more importantly `in | xlsx2csv -`.

This commit only adds support for python3 and will fall back to the
previous behaviour for python2. For compatibility reasons it does not
use type=argparse.FileType('r'), but parses "-" manually.

Fixes: dilshod#263
Signed-off-by: Bence Ferdinandy <bence@ferdinandy.com>
@ferdinandyb
Copy link
Copy Markdown
Contributor Author

Credit where credit's due: https://stackoverflow.com/a/76780830/2241241

@dilshod dilshod merged commit 0119a8c into dilshod:master Jul 31, 2023
@Konfekt
Copy link
Copy Markdown
Contributor

Konfekt commented Dec 6, 2023

Hello, was there a test before this went productive? How can it possibly work in view of

#  xlsx2csv.py (lines 1216-1230)
try:
    if os.path.isdir(options.infile):
        convert_recursive(options.infile, sheetid, outfile, kwargs)
    elif not os.path.exists(options.infile):
        raise InvalidXlsxFileException("Input file not found!")
    else:
        xlsx2csv = Xlsx2csv(options.infile, **kwargs)
        if options.sheetname:
            sheetid = xlsx2csv.getSheetIdByName(options.sheetname)
            if not sheetid:
                sys.exit("Sheet '%s' not found" % options.sheetname)
        xlsx2csv.convert(outfile, sheetid)
except XlsxException:
    _, e, _ = sys.exc_info()
    sys.exit(str(e) + "\n")

Where is this documented, say in the output --help?

@Konfekt
Copy link
Copy Markdown
Contributor

Konfekt commented Dec 6, 2023

That said, it would be a useful feature as Excel files are still common in many companies and https://github.com/phiresky/ripgrep-all/ is missing an adapter for those reading them from STDIn.

@ferdinandyb
Copy link
Copy Markdown
Contributor Author

ferdinandyb commented Dec 6, 2023 via email

@Konfekt
Copy link
Copy Markdown
Contributor

Konfekt commented Dec 6, 2023

Thank you. When I run this against cat file.xlsx | xlsx2csv - with latest xlsx2csv it complains about no found input file. Indeed, as by

#  xlsx2csv.py (line 1140)
options.infile = args[0]

it is simply the first non-option argument?

@ferdinandyb
Copy link
Copy Markdown
Contributor Author

ferdinandyb commented Dec 6, 2023 via email

@Konfekt
Copy link
Copy Markdown
Contributor

Konfekt commented Dec 7, 2023

It 0.8.1. To be precise, the latest commit as the release did not contain this pull request

@ferdinandyb
Copy link
Copy Markdown
Contributor Author

@Konfekt I just checked, this PR was broken at some point.

@ferdinandyb ferdinandyb mentioned this pull request Dec 12, 2023
3 tasks
@ferdinandyb
Copy link
Copy Markdown
Contributor Author

@Konfekt you can test #271, especially if you have Windows at hand it would be nice.

@Konfekt
Copy link
Copy Markdown
Contributor

Konfekt commented Dec 13, 2023

Yes, thank you, that addition

 and options.infile != "-"

in

#  xlsx2csv.py (line 1219)
elif not os.path.exists(options.infile)  and options.infile != "-":

solves it. I am (regrettably?!) not on Windows, though.

Sorry, that it didn't come to mind that the check for an existent file was added after your commit; there were few and it seemed like something that must have been there for a while.

@Konfekt
Copy link
Copy Markdown
Contributor

Konfekt commented Dec 13, 2023

This issue settled, how about a new release containing this stdin feature at some point?

@ferdinandyb
Copy link
Copy Markdown
Contributor Author

@Konfekt You'll need to ping the maintainer about that, I'm not sure he even saw this thread.

@dilshod
Copy link
Copy Markdown
Owner

dilshod commented Dec 13, 2023

I've just created a new version 0.8.2, it is uploaded to pypi

@Konfekt
Copy link
Copy Markdown
Contributor

Konfekt commented Dec 13, 2023

Thank you @dilshod ; I was a bit shy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: read xlsx from STDIN

3 participants