Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to include an excel file (.xls or .xlsx) directly #37

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions pantable/pantable.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,20 @@ def read_data(include, data):
if include is None:
with io.StringIO(data) as file:
raw_table_list = list(csv.reader(file))
elif str(include).strip().endswith('.xls') or \
str(include).strip().endswith('.xlsx'):
try:
import pandas as pd
temp = pd.read_excel(include, index_col=0)
raw_table_list = read_data(None, temp.to_csv())
except ModuleNotFoundError:
raw_table_list = None
panflute.debug("pandas needs to be installed. You "
"can find installation instruction at "
"http://pandas.pydata.org/pandas-docs/stable/install.html")
except IOError:
raw_table_list = None
panflute.debug("pantable: file not found from the path", include)
else:
try:
with io.open(str(include)) as file:
Expand Down
Binary file added tests/csv_tables.xls
Binary file not shown.
Binary file added tests/csv_tables.xlsx
Binary file not shown.
23 changes: 23 additions & 0 deletions tests/pantable/test_read_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ def test_read_data():
# check include
# invalid include: file doesn't exist
assert read_data('abc.xyz', '') is None
assert read_data('abc.xlsx', '') is None
# invalid include: wrong type
assert read_data(True, '') is None
# valid include
assert read_data('tests/csv_tables.csv', '') is not None
assert read_data('tests/csv_tables.xlsx', '') is not None
assert read_data('tests/csv_tables.xls', '') is not None
# check type
data = r"""1,2
3,4
Expand Down Expand Up @@ -41,6 +44,26 @@ def test_read_data():
'$2.10',
'5^10^~units~',
'Benefits of eating oranges:\n\n- **cures** scurvy\n- `tasty`']]
assert read_data('tests/csv_tables.xlsx',
data) == [['**_Fruit_**', '~~Price~~', '_Number_', '`Advantages`'],
['*Bananas~1~*',
'1.34',
'12~units~',
'Benefits of eating bananas \r\n(**Note the appropriately\r\nrendered block markdown**): \r\n\r\n- _built-in wrapper_ \r\n- ~~**bright color**~~\r\n\r\n'],
['*Oranges~2~*',
'2.1',
'5^10^~units~',
'Benefits of eating oranges:\r\n\r\n- **cures** scurvy\r\n- `tasty`']]
assert read_data('tests/csv_tables.xls',
data) == [['**_Fruit_**', '~~Price~~', '_Number_', '`Advantages`'],
['*Bananas~1~*',
'1.34',
'12~units~',
'Benefits of eating bananas \r\n(**Note the appropriately\r\nrendered block markdown**): \r\n\r\n- _built-in wrapper_ \r\n- ~~**bright color**~~\r\n\r\n'],
['*Oranges~2~*',
'2.1',
'5^10^~units~',
'Benefits of eating oranges:\r\n\r\n- **cures** scurvy\r\n- `tasty`']]
# check empty table
assert read_data(None, '') == []
return
1 change: 1 addition & 0 deletions tests/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ pytest-cov
coverage
coveralls
future
pandas