-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
TYP/CLN: cleanup _openpyxl.py
, add type annotation #36021
#36022
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0f70991
TYP/CLN: cleanup, add type annotation #36021
fangchenli bba2586
TYP/CLN: bump openpyxl vertion to fix import error #36021
fangchenli b81bae3
import type
fangchenli 915183c
fix importr error
fangchenli baf0966
fix importr error
fangchenli 48da88e
remove alias
fangchenli 4f0a843
move import to module scope
fangchenli 2340da6
bump openpyxl version in doc
fangchenli 3d0781a
move import back to function scope
fangchenli 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,4 +1,4 @@ | ||
from typing import List | ||
from typing import TYPE_CHECKING, Dict, List, Optional | ||
|
||
import numpy as np | ||
|
||
|
@@ -8,6 +8,9 @@ | |
from pandas.io.excel._base import ExcelWriter, _BaseExcelReader | ||
from pandas.io.excel._util import _validate_freeze_panes | ||
|
||
if TYPE_CHECKING: | ||
from openpyxl.descriptors.serialisable import Serialisable | ||
|
||
|
||
class _OpenpyxlWriter(ExcelWriter): | ||
engine = "openpyxl" | ||
|
@@ -22,53 +25,22 @@ def __init__(self, path, engine=None, mode="w", **engine_kwargs): | |
if self.mode == "a": # Load from existing workbook | ||
from openpyxl import load_workbook | ||
|
||
book = load_workbook(self.path) | ||
self.book = book | ||
self.book = load_workbook(self.path) | ||
else: | ||
# Create workbook object with default optimized_write=True. | ||
self.book = Workbook() | ||
|
||
if self.book.worksheets: | ||
try: | ||
self.book.remove(self.book.worksheets[0]) | ||
except AttributeError: | ||
|
||
# compat - for openpyxl <= 2.4 | ||
self.book.remove_sheet(self.book.worksheets[0]) | ||
self.book.remove(self.book.worksheets[0]) | ||
|
||
def save(self): | ||
""" | ||
Save workbook to disk. | ||
""" | ||
return self.book.save(self.path) | ||
|
||
@classmethod | ||
def _convert_to_style(cls, style_dict): | ||
""" | ||
Converts a style_dict to an openpyxl style object. | ||
|
||
Parameters | ||
---------- | ||
style_dict : style dictionary to convert | ||
""" | ||
from openpyxl.style import Style | ||
|
||
xls_style = Style() | ||
for key, value in style_dict.items(): | ||
for nk, nv in value.items(): | ||
if key == "borders": | ||
( | ||
xls_style.borders.__getattribute__(nk).__setattr__( | ||
"border_style", nv | ||
) | ||
) | ||
else: | ||
xls_style.__getattribute__(key).__setattr__(nk, nv) | ||
|
||
return xls_style | ||
self.book.save(self.path) | ||
|
||
@classmethod | ||
def _convert_to_style_kwargs(cls, style_dict): | ||
def _convert_to_style_kwargs(cls, style_dict: dict) -> Dict[str, "Serialisable"]: | ||
""" | ||
Convert a style_dict to a set of kwargs suitable for initializing | ||
or updating-on-copy an openpyxl v2 style object. | ||
|
@@ -93,7 +65,7 @@ def _convert_to_style_kwargs(cls, style_dict): | |
""" | ||
_style_key_map = {"borders": "border"} | ||
|
||
style_kwargs = {} | ||
style_kwargs: Dict[str, Serialisable] = {} | ||
for k, v in style_dict.items(): | ||
if k in _style_key_map: | ||
k = _style_key_map[k] | ||
|
@@ -404,7 +376,7 @@ def write_cells( | |
# Write the frame cells using openpyxl. | ||
sheet_name = self._get_sheet_name(sheet_name) | ||
|
||
_style_cache = {} | ||
_style_cache: Dict[str, Dict[str, Serialisable]] = {} | ||
|
||
if sheet_name in self.sheets: | ||
wks = self.sheets[sheet_name] | ||
|
@@ -426,7 +398,7 @@ def write_cells( | |
if fmt: | ||
xcell.number_format = fmt | ||
|
||
style_kwargs = {} | ||
style_kwargs: Optional[Dict[str, Serialisable]] = {} | ||
if cell.style: | ||
key = str(cell.style) | ||
style_kwargs = _style_cache.get(key) | ||
|
@@ -515,16 +487,17 @@ def get_sheet_by_index(self, index: int): | |
|
||
def _convert_cell(self, cell, convert_float: bool) -> Scalar: | ||
|
||
# TODO: replace with openpyxl constants | ||
from openpyxl.cell.cell import TYPE_BOOL, TYPE_ERROR, TYPE_NUMERIC | ||
fangchenli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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. openpyxl verion < 2.6 would raise import error here. |
||
if cell.is_date: | ||
return cell.value | ||
elif cell.data_type == "e": | ||
elif cell.data_type == TYPE_ERROR: | ||
return np.nan | ||
elif cell.data_type == "b": | ||
elif cell.data_type == TYPE_BOOL: | ||
return bool(cell.value) | ||
elif cell.value is None: | ||
return "" # compat with xlrd | ||
elif cell.data_type == "n": | ||
elif cell.data_type == TYPE_NUMERIC: | ||
# GH5394 | ||
if convert_float: | ||
val = int(cell.value) | ||
|
This file contains hidden or 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.
I don't see this method is used anywhere. And it doesn't affect any test. Also, it seems like openpyxl doesn't have a
style
module anymore.