Skip to content

Commit

Permalink
Change pandas ExcelWriter engine to openpyxl.
Browse files Browse the repository at this point in the history
-Eliminates need for `XlsxWriter` package.
  • Loading branch information
JS3xton committed May 7, 2020
1 parent 9cd83ef commit 95b3ac1
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 18 deletions.
26 changes: 12 additions & 14 deletions FlowCal/excel_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
from matplotlib import pyplot as plt
import numpy as np
import pandas as pd
import openpyxl

import FlowCal.io
import FlowCal.plot
Expand Down Expand Up @@ -183,7 +184,7 @@ def write_workbook(filename, table_list, column_width=None):
Tables to be saved as individual sheets in the Excel table. Each
tuple contains two values: the name of the sheet to be saved as a
string, and the contents of the table as a DataFrame.
column_width: int, optional
column_width: int or float, optional
The column width to use when saving the spreadsheet. If None,
calculate width automatically from the maximum number of characters
in each column.
Expand Down Expand Up @@ -216,7 +217,7 @@ def write_workbook(filename, table_list, column_width=None):
pass

# Generate output writer object
writer = pd.ExcelWriter(filename, engine='xlsxwriter')
writer = pd.ExcelWriter(filename, engine='openpyxl')

# Write tables
for sheet_name, df in table_list:
Expand All @@ -225,21 +226,18 @@ def write_workbook(filename, table_list, column_width=None):
# Write to an Excel sheet
df.to_excel(writer, sheet_name=sheet_name, index=False)
# Set column width
if column_width is None:
for i, (col_name, column) in enumerate(six.iteritems(df)):
for i, (col_name, column) in enumerate(six.iteritems(df)):
if column_width is None:
# Get the maximum number of characters in a column
max_chars_col = column.astype(str).str.len().max()
max_chars_col = max(len(col_name), max_chars_col)
# Write width
writer.sheets[sheet_name].set_column(
i,
i,
width=1.*max_chars_col)
else:
writer.sheets[sheet_name].set_column(
0,
len(df.columns) - 1,
width=column_width)
width = float(max_chars_col)
else:
width = float(column_width)

# Write width
col_letter = openpyxl.utils.get_column_letter(i+1)
writer.sheets[sheet_name].column_dimensions[col_letter].width = width

# Write excel file
writer.save()
Expand Down
4 changes: 2 additions & 2 deletions doc/getting_started/install_python.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Alternatively, download ``FlowCal`` from `here <https://github.com/taborlab/Flow
* ``scikit-learn`` (>=0.16.0)
* ``pandas`` (>=0.16.1)
* ``xlrd`` (>=0.9.2)
* ``XlsxWriter`` (>=0.5.2)
* ``openpyxl`` (>=2.4.1)

If you have ``pip``, a ``requirements.txt`` file is provided, such that the required packages can be installed by running::

Expand Down Expand Up @@ -52,4 +52,4 @@ Again, some users may need to precede the previous commands with ``sudo``.

sudo pip install --upgrade pip

After this, you may install ``FlowCal`` by following the steps above.
After this, you may install ``FlowCal`` by following the steps above.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ scikit-image>=0.10.0
scikit-learn>=0.16.0
pandas>=0.16.1
xlrd>=0.9.2
XlsxWriter>=0.5.2
openpyxl>=2.4.1
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def find_version(file_path):
'scikit-learn>=0.16.0',
'pandas>=0.16.1',
'xlrd>=0.9.2',
'XlsxWriter>=0.5.2'],
'openpyxl>=2.4.1'],

# List additional groups of dependencies here (e.g. development
# dependencies). You can install these using the following syntax,
Expand Down

0 comments on commit 95b3ac1

Please sign in to comment.