Skip to content

Reference for ExcelParser

The ExcelParser enables parsing spreadsheets, such as .xslx files. All sheets in the notebook will be parsed.

Bases: AbstractSheetParser[bytes]

Parser for spreadsheets, such as Excel workbooks (.xslx). For a list of handled filetypes, refer to https://pandas.pydata.org/docs/reference/api/pandas.read_excel.html

__init__(output_format='auto', n_sample_rows=1000)

Initializes an Excel parser

Parameters:

Name Type Description Default
output_format Literal["markdown_table", "json_lines", "auto]

the output format of the parsed document. - markdown_table : uses tabula to build a markdown-formatted table. - json_lines : each row of the table will be output as a JSON line. Better for chunking as headers are preserved. - auto : will detect which format is the more suitable. CSV-like sheet will be converset to JSON lines. Defaults to "auto".

'auto'
n_sample_rows int

number of rows sampled at the top of each sheet to compute its SheetProfile. Defaults to 1000.

1000

convert_sheets_to_output_format(sheets)

Handle the conversion of the sheets obtained from pandas.read_excel() method to the specified output format. The profile of each sheet is stored in self.sheet_profiles.

Parameters:

Name Type Description Default
sheets dict[str, DataFrame]

the sheets returned from pd.read_excel(sheet_name=None).

required

Returns:

Name Type Description
str str

the formatted string

inspect_file(filepath)

Describes the structure of each sheet of an excel-like file, without loading it entirely. Useful to route the file before parsing, e.g. to handle long CSV-like sheets differently.

Parameters:

Name Type Description Default
filepath str

path to the file.

required

Returns:

Type Description
dict[str, SheetProfile]

dict[str, SheetProfile]: a mapping containing {sheet_name: corresponding_profile}.

inspect_string(string)

Describes the structure of each sheet of an excel file, without loading it entirely. Useful to route the file before parsing, e.g. to handle long CSV-like sheets differently.

Parameters:

Name Type Description Default
string bytes

the excel as a byte string.

required

Returns:

Type Description
dict[str, SheetProfile]

dict[str, SheetProfile]: a mapping containing {sheet_name: corresponding_profile}.

parse_file(filepath)

Parses a excel-like file to markdown.

Parameters:

Name Type Description Default
filepath str

the path to the excel-like file.

required

Returns:

Name Type Description
MarkdownDoc MarkdownDoc

the markdown formatted excel file.

parse_string(string)

Parses a bytes string representing an excel file.

Parameters:

Name Type Description Default
string bytes

the excel as a byte string.

required

Returns:

Name Type Description
MarkdownDoc MarkdownDoc

the markdown formatted excel file

read_file(filepath)

Read the provided filepath.

Parameters:

Name Type Description Default
filepath str

path to the file.

required

Returns:

Type Description
dict[str, DataFrame]

dict[str, pd.DataFrame]: a mapping containing {sheet_name: corresponding_dataframe.}

Inspecting a file before parsing

ExcelParser.inspect_file() (or inspect_string() for bytes) describes each sheet without loading it entirely: the metrics are computed on the first n_sample_rows rows, and the row count is read from the file metadata. It can be used to route very long CSV-like files before parsing them.

from chunknorris.parsers import ExcelParser

profiles = ExcelParser().inspect_file("myfile.xlsx")
for sheet_name, profile in profiles.items():
    print(sheet_name, profile.n_rows, profile.is_csv_like)

Structural description of a sheet, used to tell CSV-like sheets (a header row followed by homogeneous records) from dashboard-like ones (titles, merged cells, sparse and irregular layout).

The metrics are computed on a sample of the first rows, so that a profile can be obtained without loading very long sheets.

Attributes:

Name Type Description
name str

the name of the sheet.

n_rows int | None

number of data rows (header excluded), read from the file metadata. May include trailing empty rows that are formatted. None if the format does not store it (.xls, .ods...) or if it is inconsistent.

n_columns int

number of non-empty columns.

data_density float

share of non-empty cells, in [0, 1].

header_quality float

share of columns with a proper name, in [0, 1].

row_consistency float

1 - coefficient of variation of the number of non-empty cells per row. 1 means all rows are equally filled.

is_csv_like property

Whether the sheet is a CSV-like table, i.e. a header row followed by homogeneous records.

from_dataframe(name, df, n_rows=None) classmethod

Computes the profile of a sheet.

Parameters:

Name Type Description Default
name str

the name of the sheet.

required
df DataFrame

the sheet, or a sample of its first rows, as returned by pd.read_excel().

required
n_rows int | None

the number of data rows of the whole sheet, if known. Defaults to None.

None

Returns:

Name Type Description
SheetProfile SheetProfile

the profile of the sheet.