-
-
Notifications
You must be signed in to change notification settings - Fork 593
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
feat: support column_width in xlsx format #516
Open
Birdi7
wants to merge
13
commits into
jazzband:master
Choose a base branch
from
Birdi7:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ddfee5d
feat: support column_width in xlxs format
Birdi7 b7d313e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ead09ee
Update docs/formats.rst
Birdi7 3bcf8aa
chore: add tests for adaptive column width in xlsx format
Birdi7 2ef0655
Merge branch 'master' of github.com:jazzband/tablib
Birdi7 79f3593
chore: change assert style
Birdi7 7e59454
Update src/tablib/formats/_xlsx.py
Birdi7 74eacdb
Update src/tablib/formats/_xlsx.py
Birdi7 bed476c
chore: alphabetical order in AUTHORS
Birdi7 45ae8ca
chore: docs
Birdi7 5a458ac
chore: add tests and fix bug
Birdi7 2cec185
refactor: helper function for tests and new test case for error
Birdi7 0f7e2ef
chore: use from __future__ import annotations
Birdi7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,5 +1,6 @@ | ||||||||||||||||
""" Tablib - XLSX Support. | ||||||||||||||||
""" | ||||||||||||||||
from __future__ import annotations | ||||||||||||||||
|
||||||||||||||||
import re | ||||||||||||||||
from io import BytesIO | ||||||||||||||||
|
@@ -35,12 +36,17 @@ def detect(cls, stream): | |||||||||||||||
return False | ||||||||||||||||
|
||||||||||||||||
@classmethod | ||||||||||||||||
def export_set(cls, dataset, freeze_panes=True, invalid_char_subst="-"): | ||||||||||||||||
def export_set( | ||||||||||||||||
cls, dataset, freeze_panes=True, invalid_char_subst="-", | ||||||||||||||||
column_width: str | int | None = "adaptive" | ||||||||||||||||
): | ||||||||||||||||
"""Returns XLSX representation of Dataset. | ||||||||||||||||
|
||||||||||||||||
If dataset.title contains characters which are considered invalid for an XLSX file | ||||||||||||||||
sheet name (http://www.excelcodex.com/2012/06/worksheets-naming-conventions/), they will | ||||||||||||||||
be replaced with `invalid_char_subst`. | ||||||||||||||||
|
||||||||||||||||
column_width: can be None, an integer, or "adaptive" | ||||||||||||||||
""" | ||||||||||||||||
wb = Workbook() | ||||||||||||||||
ws = wb.worksheets[0] | ||||||||||||||||
|
@@ -51,6 +57,11 @@ def export_set(cls, dataset, freeze_panes=True, invalid_char_subst="-"): | |||||||||||||||
) | ||||||||||||||||
|
||||||||||||||||
cls.dset_sheet(dataset, ws, freeze_panes=freeze_panes) | ||||||||||||||||
if isinstance(column_width, str) and column_width != "adaptive": | ||||||||||||||||
raise ValueError(f"Unsupported value `{column_width}` passed to `column_width` " | ||||||||||||||||
"parameter. It supports 'adaptive' or integer values") | ||||||||||||||||
Comment on lines
+61
to
+62
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. Maybe something like this, to be closer to existing exceptions? And let's start assigning exception messages to a variable first, for the reasons set out at https://github.com/henryiii/flake8-errmsg
Suggested change
|
||||||||||||||||
|
||||||||||||||||
cls._adapt_column_width(ws, column_width) | ||||||||||||||||
|
||||||||||||||||
stream = BytesIO() | ||||||||||||||||
wb.save(stream) | ||||||||||||||||
|
@@ -166,3 +177,25 @@ def dset_sheet(cls, dataset, ws, freeze_panes=True): | |||||||||||||||
cell.value = col | ||||||||||||||||
except (ValueError, TypeError): | ||||||||||||||||
cell.value = str(col) | ||||||||||||||||
|
||||||||||||||||
@classmethod | ||||||||||||||||
def _adapt_column_width(cls, worksheet, | ||||||||||||||||
width: str | int | None) -> None: | ||||||||||||||||
if width is None: | ||||||||||||||||
return | ||||||||||||||||
|
||||||||||||||||
column_widths = [] | ||||||||||||||||
if isinstance(width, str) and width == "adaptive": | ||||||||||||||||
for row in worksheet.values: | ||||||||||||||||
for i, cell in enumerate(row): | ||||||||||||||||
cell = str(cell) | ||||||||||||||||
if len(column_widths) > i: | ||||||||||||||||
if len(cell) > column_widths[i]: | ||||||||||||||||
column_widths[i] = len(cell) | ||||||||||||||||
else: | ||||||||||||||||
column_widths += [len(cell)] | ||||||||||||||||
else: | ||||||||||||||||
column_widths = [width] * worksheet.max_column | ||||||||||||||||
|
||||||||||||||||
for i, column_width in enumerate(column_widths, 1): # start at 1 | ||||||||||||||||
worksheet.column_dimensions[get_column_letter(i)].width = column_width |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
3.3.0 has been released, let's target 3.4.0: