-
-
Save searchzakir/5cb911b712f104f6a6b9 to your computer and use it in GitHub Desktop.
Converts a CSV (tab delimited) file to an Excel xlsx file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
FUNCTION: Converts a CSV (tab delimited) file to an Excel xlsx file. | |
Copyright (c) 2012, Konrad Foerstner <[email protected]> | |
Permission to use, copy, modify, and/or distribute this software for | |
any purpose with or without fee is hereby granted, provided that the | |
above copyright notice and this permission notice appear in all | |
copies. | |
THE SOFTWARE IS PROVIDED 'AS IS' AND THE AUTHOR DISCLAIMS ALL | |
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED | |
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE | |
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | |
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | |
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER | |
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | |
PERFORMANCE OF THIS SOFTWARE. | |
""" | |
import argparse | |
import csv | |
import sys | |
from openpyxl.workbook import Workbook | |
parser = argparse.ArgumentParser() | |
parser.add_argument("input_file") | |
args = parser.parse_args() | |
if not ".csv" in args.input_file: | |
sys.stderr.write("Error: File does not have the ending \".csv\".\n") | |
sys.exit(2) | |
input_fh = open(args.input_file) | |
workbook = Workbook() | |
sheet = workbook.create_sheet(0) | |
for row_index, row in enumerate( | |
csv.reader(open(args.input_file), delimiter="\t")): | |
for col_index, col in enumerate(row): | |
sheet.cell(row = row_index, column = col_index).value = col | |
workbook.save(open(args.input_file.replace(".csv", ".xlsx") , "w")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment