2.1.1.5. wc_utils.workbook package¶
2.1.1.5.1. Submodules¶
2.1.1.5.2. wc_utils.workbook.core module¶
Excel utilities
- Author
Jonathan Karr <karr@mssm.edu>
- Date
2016-11-28
- Copyright
2016-2018, Karr Lab
- License
MIT
-
class
wc_utils.workbook.core.
Row
[source]¶ Bases:
list
Represents a row in a table of data
-
__eq__
(other)[source]¶ Compare rows
- Parameters
other (
Row
) – other row- Returns
True if rows are semantically equal
- Return type
bool
-
__getitem__
(i_cell)[source]¶ Get a cell or a range of cells
- Parameters
i_cell (
int
ofslice
) – cell index or range of cell indices- Returns
cell or range of cells
- Return type
object
orRow
-
__ne__
(other)[source]¶ Compare rows
- Parameters
other (
Row
) – other row- Returns
True if rows are semantically unequal
- Return type
bool
-
cell_difference
(cell_self, cell_other)[source]¶ Get difference between cells
- Parameters
cell_self (
object
) – self cellcell_other (
object
) – other cell
- Returns
difference
- Return type
-
-
class
wc_utils.workbook.core.
RowDifference
[source]¶ Bases:
collections.OrderedDict
Difference between values of rows
-
class
wc_utils.workbook.core.
Workbook
[source]¶ Bases:
collections.OrderedDict
Represents an Excel workbook
-
__eq__
(other)[source]¶ Compare two workbooks
- Parameters
other (
Workbook
) – other workbook- Returns
true if workbooks are semantically equal
- Return type
bool
-
-
class
wc_utils.workbook.core.
WorkbookDifference
[source]¶ Bases:
dict
Difference between values of workbook
-
class
wc_utils.workbook.core.
Worksheet
[source]¶ Bases:
list
Represents a table of data, such as an Excel worksheet or a csv/tsv file
-
__eq__
(other)[source]¶ Compare two worksheets
- Parameters
other (
Worksheet
) – other worksheet- Returns
True if worksheets are semantically equal
- Return type
bool
-
__ne__
(other)[source]¶ Compare two worksheets
- Parameters
other (
Worksheet
) – other worksheet- Returns
True if worksheets are semantically unequal
- Return type
bool
-
2.1.1.5.3. wc_utils.workbook.io module¶
IO utilities
- Author
Jonathan Karr <karr@mssm.edu>
- Author
Arthur Goldberg <Arthur.Goldberg@mssm.edu>
- Date
2016-11-28
- Copyright
2016-2018, Karr Lab
- License
MIT
-
class
wc_utils.workbook.io.
ExcelReader
(path)[source]¶ Bases:
wc_utils.workbook.io.Reader
Read data from Excel file
-
get_sheet_names
()[source]¶ Get names of sheets contained within path
- Returns
list of str: list of sheet names
- Return type
obj
-
read_cell
(sheet_name, xls_worksheet, i_row, i_col)[source]¶ Read the value of a cell
- Parameters
sheet_name (
str
) – worksheet namexls_worksheet (
openpyxl.Worksheet
) – worksheeti_row (
int
) – row numberi_col (
int
) – column number
- Returns
value of cell
- Return type
object
-
read_worksheet
(sheet_name, ignore_empty_final_rows=True, ignore_empty_final_cols=True)[source]¶ Read data from Excel worksheet
- Parameters
sheet_name (
str
) – sheet nameignore_empty_final_rows (
bool
, optional) – ifTrue
, ignore empty final rowsignore_empty_final_cols (
bool
, optional) – ifTrue
, ignore empty final columns
- Returns
data
- Return type
Worksheet
- Raises
ValueError –
-
-
class
wc_utils.workbook.io.
ExcelWriter
(path, title=None, description=None, keywords=None, version=None, language=None, creator=None)[source]¶ Bases:
wc_utils.workbook.io.Writer
Write data to Excel file
-
write_cell
(xls_worksheet, sheet_name, i_row, i_col, value, format)[source]¶ Write a value to a cell
- Parameters
xls_worksheet (
xlsxwriter.Worksheet
) – Excel worksheetsheet_name (
str
) – sheet namei_row (
int
) – row of cell to writei_col (
int
) – column of cell to writevalue (
object
) – value to writeformat (
xlsxwriter.Format
) – format for the cell
-
write_worksheet
(sheet_name, data, style=None, validation=None, protected=False, include_help_comments=False)[source]¶ Write worksheet to file
- Parameters
sheet_name (
str
) – sheet namedata (
Worksheet
) – python representation of data; each element must be a string, boolean, integer, float, or NoneTypestyle (
WorksheetStyle
, optional) – worksheet stylevalidation (
WorksheetValidation
, optional) – worksheet validationprotected (
bool
, optional) – ifTrue
, protect the worksheetinclude_help_comments (
bool
, optional) – ifTrue
, include help comments
-
-
class
wc_utils.workbook.io.
FieldValidation
(input_title='', input_message='', show_input=True, type=<FieldValidationType.any: 8>, criterion=None, allowed_scalar_value=None, minimum_scalar_value=None, maximum_scalar_value=None, allowed_list_values=None, show_dropdown=True, ignore_blank=True, error_type=<FieldValidationErrorType.warning: 2>, error_title='', error_message='', show_error=True)[source]¶ Bases:
object
Validation for row- or column-oriented field
-
apply_help_comment
(ws, i_row, i_col)[source]¶ Apply help comment to cell
- Parameters
ws (
xlsxwriter.Worksheet
) – worksheeti_row (
int
) – rowi_col (
int
) – column
-
-
class
wc_utils.workbook.io.
FieldValidationErrorType
[source]¶ Bases:
int
,enum.Enum
Type of error dialog to display
-
class
wc_utils.workbook.io.
Hyperlink
(i_row, i_col, url, tip=None)[source]¶ Bases:
object
Hyperlink from a cell
-
i_row
[source] column
- Type
col
-
-
class
wc_utils.workbook.io.
Reader
(path)[source]¶ Bases:
object
Read data from file(s)
-
abstract
get_sheet_names
()[source]¶ Get names of sheets contained within path
- Returns
list of str: list of sheet names
- Return type
obj
-
abstract
read_worksheet
(sheet_name, ignore_empty_final_rows=True, ignore_empty_final_cols=True)[source]¶ Read data from file
- Parameters
sheet_name (
str
) – sheet nameignore_empty_final_rows (
bool
, optional) – ifTrue
, ignore empty final rowsignore_empty_final_cols (
bool
, optional) – ifTrue
, ignore empty final columns
- Returns
data
- Return type
Worksheet
-
abstract
-
class
wc_utils.workbook.io.
SeparatedValuesReader
(path)[source]¶ Bases:
wc_utils.workbook.io.Reader
Read data from csv/tsv file(s)
-
get_sheet_names
()[source]¶ Get names of files contained within path glob
- Returns
list of str: list of file names
- Return type
obj
- Raises
ValueError – if glob does not find any matching files
-
read_cell
(value)[source]¶ Read the value of a cell
- Parameters
value (
object
) – value- Returns
value
- Return type
object
-
read_worksheet
(sheet_name, ignore_empty_final_rows=True, ignore_empty_final_cols=True)[source]¶ Read data from file
- Parameters
sheet_name (
str
) – sheet nameignore_empty_final_rows (
bool
, optional) – ifTrue
, ignore empty final rowsignore_empty_final_cols (
bool
, optional) – ifTrue
, ignore empty final columns
- Returns
data
- Return type
Worksheet
-
-
class
wc_utils.workbook.io.
SeparatedValuesWriter
(path, title=None, description=None, keywords=None, version=None, language=None, creator=None)[source]¶ Bases:
wc_utils.workbook.io.Writer
Write data to csv/tsv file(s)
-
run
(data, style=None, validation=None, protected=False)[source]¶ Write workbook to file(s)
- Parameters
data (
Workbook
) – python representation of data; each element must be a string, boolean, integer, float, or NoneTypestyle (
WorkbookStyle
, optional) – workbook stylevalidation (
WorkbookValidation
, optional) – validationprotected (
bool
, optional) – ifTrue
, protect the worksheet
-
write_worksheet
(sheet_name, data, style=None, validation=None, protected=False)[source]¶ Write worksheet to file
- Parameters
sheet_name (
str
) – sheet namedata (
Worksheet
) – python representation of data; each element must be a string, boolean, integer, float, or NoneTypestyle (
WorksheetStyle
, optional) – worksheet stylevalidation (
WorksheetValidation
, optional) – worksheet validationprotected (
bool
, optional) – ifTrue
, protect the worksheet
-
-
class
wc_utils.workbook.io.
WorkbookStyle
[source]¶ Bases:
dict
Workbook style: dictionary of worksheet styles
-
class
wc_utils.workbook.io.
WorkbookValidation
[source]¶ Bases:
dict
Workbook validation: dictionary of worksheet validations
-
class
wc_utils.workbook.io.
WorksheetStyle
(title_rows=0, title_row_font_bold=True, title_row_fill_pattern='solid', title_row_fill_fgcolor='888888', head_rows=0, head_columns=0, head_row_font_bold=True, head_row_fill_pattern='solid', head_row_fill_fgcolor='CCCCCC', blank_head_fill_fgcolor='', merged_head_fill_fgcolor='AAAAAA', extra_rows=inf, extra_columns=inf, font_family='Arial', font_size=11.0, row_height=15.01, col_width=15.0, auto_filter=True, merge_ranges=None, hyperlinks=None)[source]¶ Bases:
object
Worksheet style
-
class
wc_utils.workbook.io.
WorksheetValidation
(orientation=<WorksheetValidationOrientation.row: 1>, fields=None)[source]¶ Bases:
object
List of field validations
-
fields
[source]¶ field validations
- Type
list
ofFieldValidation
-
apply
(ws, first_row, first_col, last_row, last_col, include_help_comments=False)[source]¶ Apply validation to worksheet
- Parameters
ws (
xlsxwriter.Worksheet
) – worksheetfirst_row (
int
) – first rowfirst_col (
int
) – first columnlast_row (
int
) – last rowlast_col (
int
) – last columninclude_help_comments (
bool
, optional) – ifTrue
, include help comments
-
-
class
wc_utils.workbook.io.
WorksheetValidationOrientation
[source]¶ Bases:
int
,enum.Enum
Worksheet validation orientation
-
class
wc_utils.workbook.io.
Writer
(path, title=None, description=None, keywords=None, version=None, language=None, creator=None)[source]¶ Bases:
object
Write data to file(s)
-
run
(data, style=None, validation=None, protected=False)[source]¶ Write workbook to file(s)
- Parameters
data (
Workbook
) – python representation of data; each element must be a string, boolean, integer, float, or NoneTypestyle (
WorkbookStyle
, optional) – workbook stylevalidation (
WorkbookValidation
, optional) – validationprotected (
bool
, optional) – ifTrue
, protect the worksheet
-
abstract
write_worksheet
(sheet_name, data, style=None, validation=None, protected=False)[source]¶ Write worksheet to file
- Parameters
sheet_name (
str
) – sheet namedata (
Worksheet
) – python representation of data; each element must be a string, boolean, integer, float, or NoneTypestyle (
WorksheetStyle
, optional) – worksheet stylevalidation (
WorksheetValidation
, optional) – worksheet validationprotected (
bool
, optional) – ifTrue
, protect the worksheet
-
-
wc_utils.workbook.io.
convert
(source, destination, worksheet_order=None, style=None, ignore_extra_sheets=True)[source]¶ Convert among Excel (.xlsx), comma separated (.csv), and tab separated formats (.tsv)
- Parameters
source (
str
) – path to source filedestination (
str
) – path to save converted fileworksheet_order (
list
ofstr
) – worksheet orderstyle (
WorkbookStyle
, optional) – workbook style for Excelignore_extra_sheets (
bool
, optional) – true/false should extra sheets in worksheet_order be ignored or should an error be thrown
- Raises
ValueError – if file extensions are not supported or file names are equal
-
wc_utils.workbook.io.
get_reader
(extension)[source]¶ Get reader
- Parameters
extension (
str
) – extension- Returns
reader class
- Return type
class
- Raises
ValueError – if extension is not one of “.xlsx”, “.csv”, or “.tsv”
-
wc_utils.workbook.io.
get_writer
(extension)[source]¶ Get writer
- Parameters
extension (
str
) – extension- Returns
writer class
- Return type
class
- Raises
ValueError – if extension is not one of “.xlsx”, “.csv”, or “.tsv”
-
wc_utils.workbook.io.
read
(path)[source]¶ Read data from Excel (.xlsx) file or collection of comma separated (.csv) or tab separated (.tsv) file(s)
- Parameters
path (
str
) – path to file(s)- Returns
python representation of data
- Return type
Workbook
-
wc_utils.workbook.io.
write
(path, workbook, title=None, description=None, keywords=None, version=None, language=None, creator=None, style=None)[source]¶ Write data to Excel (.xlsx) file or collection of comma separated (.csv) or tab separated (.tsv) file(s)
- Parameters
path (
str
) – path to file(s)workbook (
Workbook
) – python representation of data; each element must be a string, boolean, integer, float, or NoneTypetitle (
str
, optional) – titledescription (
str
, optional) – descriptionkeywords (
str
, optional) – keywordsversion (
str
, optional) – versionlanguage (
str
, optional) – languagecreator (
str
, optional) – creatorstyle (
WorkbookStyle
, optional) – workbook style