-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move workbook to csv sheet to public repository
- Loading branch information
0 parents
commit c813ce9
Showing
5 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
exports/*.csv | ||
|
||
~$* |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Export Worksheets from Excel to CSV | ||
|
||
This script will help you to extract Worksheets from an Excel file. It already saved me days of my lifetime! | ||
|
||
## Run the example | ||
|
||
Clone this repository and try to run the example | ||
|
||
1. `git clone [email protected]:zauberware/excel-worksheets-to-csv.git && cd excel-worksheets-to-csv` | ||
2. `python worksheet_to_csv.py example.xlsx` | ||
3. See results under `exports/` | ||
|
||
|
||
## Prerequisites | ||
|
||
* Be sure `openpyxl` and `imp` is installed. | ||
* Convert your file into XLSX format if needed. It not works with other excel formats. |
Binary file not shown.
Empty 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#!/usr/bin/env python | ||
|
||
# export data sheets from xlsx to csv | ||
|
||
from openpyxl import load_workbook | ||
from imp import reload | ||
import csv | ||
import sys | ||
|
||
export_path = 'exports/' | ||
|
||
if sys.version[0] == '2': | ||
reload(sys) | ||
sys.setdefaultencoding('utf-8') | ||
|
||
def get_all_sheets(excel_file): | ||
sheets = [] | ||
workbook = load_workbook(excel_file,True,True) | ||
print(workbook.sheetnames) | ||
all_worksheets = workbook.sheetnames | ||
for worksheet_name in all_worksheets: | ||
sheets.append(worksheet_name) | ||
return sheets | ||
|
||
def csv_from_excel(excel_file, sheets): | ||
workbook = load_workbook(excel_file,True,True) | ||
for worksheet_name in sheets: | ||
print("Export " + worksheet_name + " ...") | ||
|
||
try: | ||
worksheet = workbook[worksheet_name] | ||
except KeyError: | ||
print("Could not find " + worksheet_name) | ||
sys.exit(1) | ||
|
||
your_csv_file = open(''.join([export_path, worksheet_name, '.csv']), 'w') | ||
wr = csv.writer(your_csv_file, delimiter=';',quoting=csv.QUOTE_NONNUMERIC) | ||
for row in worksheet.iter_rows(): | ||
lrow = [] | ||
for cell in row: | ||
lrow.append(cell.value) | ||
wr.writerow(lrow) | ||
print(" ... done") | ||
your_csv_file.close() | ||
|
||
if not 2 <= len(sys.argv) <= 3: | ||
print("Call with " + sys.argv[0] + " <xlxs file> [comma separated list of sheets to export]") | ||
sys.exit(1) | ||
else: | ||
sheets = [] | ||
if len(sys.argv) == 3: | ||
sheets = sys.argv[2].split(',') | ||
else: | ||
sheets = get_all_sheets(sys.argv[1]) | ||
print(sheets) | ||
assert(sheets != None and len(sheets) > 0) | ||
csv_from_excel(sys.argv[1], sheets) |