Created
February 26, 2021 01:52
-
-
Save gmirsky/f41b3b1ed123a85880e790a8b75bfba6 to your computer and use it in GitHub Desktop.
convert_excel_to_csv_and_json
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
| import pandas as pd | |
| import openpyxl | |
| from openpyxl import load_workbook | |
| workbook_name = './MyExcelWorkBook.xlsx' | |
| chars_to_remove = [ | |
| '+', | |
| '&', | |
| '(', | |
| ')', | |
| '^', | |
| '%', | |
| '.', | |
| '!', | |
| '?', | |
| ' ', | |
| ',', | |
| '\\', | |
| '/', | |
| '*', | |
| '[', | |
| ']', | |
| ':', | |
| '?'] | |
| excel_sheets = load_workbook(workbook_name, read_only=True).sheetnames | |
| #print("Sheet Names ==>", excel_sheets) | |
| for sheet in excel_sheets: | |
| sheet_name = sheet | |
| for i in chars_to_remove: | |
| sheet_name = sheet_name.replace(i, '') | |
| #print("sheet_name + .csv ==>", sheet_name + '.csv') | |
| df = pd.read_excel(workbook_name, | |
| engine='openpyxl', | |
| sheet_name=sheet) | |
| df.to_csv(sheet_name + '.csv', | |
| encoding='utf-8', | |
| index=False) | |
| df.to_json(sheet_name + '.json', | |
| orient='records') | |
| print("Done!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment