Skip to content

Commit

Permalink
Improve xlsx performance with openpyxl write_only mode
Browse files Browse the repository at this point in the history
  • Loading branch information
gwax committed Feb 22, 2023
1 parent 010a70f commit 6e961bd
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ jobs:
cache-dependency-path: |
pyproject.toml
- run: pip install -e .[lxml,dev]
- run: pip install -e .[dev]

- uses: pre-commit/action@v3.0.0
20 changes: 11 additions & 9 deletions mtg_ssm/serialization/xlsx.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def create_all_sets(sheet: Worksheet, index: ScryfallDataIndex) -> None:

def style_all_sets(sheet: Worksheet) -> None:
"""Apply styles to the all sets sheet."""
sheet.freeze_panes = sheet["C3"]
sheet.freeze_panes = "C3"
col_width_hidden_format = [
("A", 8, False, None),
("B", 30, False, None),
Expand Down Expand Up @@ -161,8 +161,11 @@ def create_all_cards(sheet: Worksheet, index: ScryfallDataIndex) -> None:

def style_all_cards(sheet: Worksheet) -> None:
"""Apply styles to the all cards sheet."""
sheet.freeze_panes = sheet["B2"]
col_width_hidden = [("A", 24, False), ("B", 32, False)]
sheet.freeze_panes = "A2"
col_width_hidden = [
("A", 28, False),
("B", 48, False),
]
for col, width, hidden in col_width_hidden:
cdim = sheet.column_dimensions[col]
cdim.width = width
Expand Down Expand Up @@ -246,7 +249,7 @@ def create_set_sheet(

def style_set_sheet(sheet: Worksheet) -> None:
"""Apply styles to a set sheet."""
sheet.freeze_panes = sheet["E2"]
sheet.freeze_panes = "E2"
col_width_hidden_format = [
("A", 5, False, None),
("B", 9, False, FORMAT_CURRENCY_USD_SIMPLE),
Expand Down Expand Up @@ -298,15 +301,15 @@ class XlsxDialect(interface.SerializationDialect):

def write(self, path: Path, collection: MagicCollection) -> None:
"""Write collection to an xlsx file."""
workbook = openpyxl.Workbook()
workbook = openpyxl.Workbook(write_only=True)

all_sets_sheet = workbook.create_sheet()
create_all_sets(all_sets_sheet, collection.oracle.index)
style_all_sets(all_sets_sheet)
create_all_sets(all_sets_sheet, collection.oracle.index)

all_cards_sheet = workbook.create_sheet()
create_all_cards(all_cards_sheet, collection.oracle.index)
style_all_cards(all_cards_sheet)
create_all_cards(all_cards_sheet, collection.oracle.index)

setcodes = [
s.code
Expand All @@ -318,9 +321,8 @@ def write(self, path: Path, collection: MagicCollection) -> None:

for setcode in setcodes:
set_sheet = workbook.create_sheet()
create_set_sheet(set_sheet, collection, setcode)
style_set_sheet(set_sheet)
del workbook["Sheet"]
create_set_sheet(set_sheet, collection, setcode)
workbook.save(str(path))

def read(self, path: Path, oracle: Oracle) -> MagicCollection:
Expand Down
4 changes: 1 addition & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ dependencies = [
"openpyxl~=3.0",
"pydantic~=1.9",
"requests~=2.27",
"lxml~=4.9"
]
dynamic = ["version"]

Expand All @@ -54,9 +55,6 @@ dynamic = ["version"]
mtg-ssm = "mtg_ssm.ssm:main"

[project.optional-dependencies]
lxml = [
"lxml>=3.7.2",
]
dev = [
"black",
"coverage[toml]",
Expand Down

0 comments on commit 6e961bd

Please sign in to comment.