Created
October 12, 2020 00:16
-
-
Save stuaxo/e6f539c010b5cd1adba9155ed9cb923f to your computer and use it in GitHub Desktop.
CSV to XLSX converter with XLSXWriter
This file contains hidden or 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
# csv_to_xlsx [filename... [filename]] | |
import csv | |
import os | |
import sys | |
import xlsxwriter | |
from pathlib import Path | |
def main(csv_filenames): | |
if non_files: | |
print("All files must exist: ", ", ".join(non_files), file=sys.stderr) | |
sys.exit(1) | |
for csv_filename in csv_filenames: | |
xl_filename = f"{Path(csv_filename).parent}/{Path(csv_filename).name[:-3]}xlsx" | |
try: | |
os.unlink(xl_filename) | |
except FileNotFoundError: | |
pass | |
with open(csv_filename, 'r') as csvfile: | |
reader = csv.reader(csvfile) | |
with xlsxwriter.Workbook(xl_filename) as workbook: | |
worksheet = workbook.add_worksheet() | |
for i, row in enumerate(reader): | |
worksheet.write_row(i, 0, row) | |
workbook.close() | |
print(xl_filename) | |
if __name__ == '__main__': | |
main(sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment