-
-
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
Changes from 5 commits
0f70991
bba2586
b81bae3
915183c
baf0966
48da88e
4f0a843
2340da6
3d0781a
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 |
---|---|---|
@@ -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 as StyleObject | ||
|
||
|
||
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): | ||
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. 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 |
||
""" | ||
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, 'StyleObject']: | ||
""" | ||
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, StyleObject] = {} | ||
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, StyleObject]] = {} | ||
|
||
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, StyleObject]] = {} | ||
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) | ||
|
Uh oh!
There was an error while loading. Please reload this page.