Skip to content

Instantly share code, notes, and snippets.

@gmirsky
Created February 26, 2021 01:52
Show Gist options
  • Select an option

  • Save gmirsky/f41b3b1ed123a85880e790a8b75bfba6 to your computer and use it in GitHub Desktop.

Select an option

Save gmirsky/f41b3b1ed123a85880e790a8b75bfba6 to your computer and use it in GitHub Desktop.
convert_excel_to_csv_and_json
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