-
Notifications
You must be signed in to change notification settings - Fork 13
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
Adding repace_table() #11
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,14 +42,57 @@ def replace_picture(self, label: str, filename: _Pathlike) -> None: | |
""" | ||
pass | ||
|
||
def replace_table(self, label: str, data) -> None: | ||
def replace_table(self, label: str, data, header=False, rownames=False): | ||
"""Replaces rectangle placeholders on one or many slides. | ||
|
||
Args: | ||
label (str): label of the placeholder (without curly braces) | ||
data (pandas.DataFrame): table to be inserted into the presentation | ||
""" | ||
pass | ||
assert isinstance(data, pandas.dataframe) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could remove the assertion here and include type hints in the method signature like so: def replace_table(self, label: str, data: pandas.dataframe, *, header: bool = False, rownames: bool = False) -> None: |
||
|
||
shapes_to_replace = self._find_shapes(label) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In #7, the method signature of |
||
|
||
rows, cols = data.shape | ||
if header: rows += 1 | ||
if rownames: cols += 1 | ||
|
||
for old_shape in shapes_to_replace: | ||
slide_shapes = old_shape._parent | ||
table = slide_shapes.add_table( | ||
rows, | ||
cols, | ||
old_shape.left, | ||
old_shape.top, | ||
old_shape.width, | ||
old_shape.height | ||
).table | ||
|
||
# set column widths | ||
col_width = Length(old_shape.width/cols) | ||
for i in range(len(table.columns)): | ||
table.columns[i].width = col_width | ||
|
||
## fill the table | ||
for c in range(data.shape[1]): | ||
for r in range(data.shape[0]): | ||
if header and r == 0: | ||
c_temp = c | ||
## when rownames, skip first column | ||
if rownames: c_temp += 1 | ||
table.cell(r, c_temp).text = data.columns[c] | ||
if rownames and c == 0: | ||
r_temp = r | ||
## when header, skip first row | ||
if header: r_temp += 1 | ||
table.cell(r_temp, c).text = str(data.index[r]) | ||
## fill table body | ||
r_shape = r | ||
if header: r_shape += 1 | ||
c_shape = c | ||
if rownames: c_shape += 1 | ||
table.cell(r_shape, c_shape).text = str(data.iloc[r, c]) | ||
|
||
|
||
def _parse_label(self, label: str) -> Tuple[Union[int, str], str]: | ||
slide_number, tag_name = label.split(':') | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We allow use kwargs only for
header
andrownames
:In my opinion it's better to use
None
instead ofFalse
. When usingFalse
as default value I assume a Boolean value is required.None
is no valid data fore these arguments and therefore it should be fine to use it. Using anUndefined()
object could be an option, too. But I think this is not necessary in this case.