Skip to content

Commit

Permalink
Optimization (#44)
Browse files Browse the repository at this point in the history
* 🔨 code refactoring

* 🔨 remove useless code

* 💚 update test case

* 💄 update coding style

* This is an auto-commit, updating project meta data, such as changelog.rst, contributors.rst

* 💄 update coding style

Co-authored-by: chfw <[email protected]>
  • Loading branch information
chfw and chfw authored Oct 8, 2020
1 parent 3369e93 commit a63a039
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 47 deletions.
11 changes: 0 additions & 11 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ python:

stages:
- lint
- moban
- test


Expand All @@ -23,16 +22,6 @@ stages:
stage: lint
script: make lint

.moban: &moban
python: 3.6
env:
- MINREQ=0
stage: moban
install: pip install moban gitfs2 pypifs moban-jinja2-github moban-ansible
script:
- moban
- git diff --exit-code

jobs:
include:
- *moban
Expand Down
1 change: 1 addition & 0 deletions pyexcel-xlsx.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ test_dependencies:
- pyexcel-xls
- pyexcel
description: A wrapper library to read, manipulate and write data in xlsx and xlsm format
moban_command: false
43 changes: 20 additions & 23 deletions pyexcel_xlsx/xlsxr.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,36 @@
Read xlsx file format using openpyxl
:copyright: (c) 2015-2019 by Onni Software Ltd & its contributors
:copyright: (c) 2015-2020 by Onni Software Ltd & its contributors
:license: New BSD License
"""
from io import BytesIO
from collections import OrderedDict

import openpyxl
from pyexcel_io._compact import OrderedDict
from pyexcel_io.plugin_api.abstract_sheet import ISheet
from pyexcel_io.plugin_api import ISheet, IReader, NamedContent


class FastSheet(ISheet):
"""
Iterate through rows
"""

def __init__(self, sheet, **_):
self.xlsx_sheet = sheet

@property
def name(self):
"""sheet name"""
return self._native_sheet.title
return self.xlsx_sheet.title

def row_iterator(self):
"""
openpyxl row iterator
http://openpyxl.readthedocs.io/en/default/optimized.html
"""
for row in self._native_sheet.rows:
for row in self.xlsx_sheet.rows:
yield row

def column_iterator(self, row):
Expand Down Expand Up @@ -65,7 +68,7 @@ class SlowSheet(FastSheet):
"""

def __init__(self, sheet, **keywords):
self._native_sheet = sheet
self.xlsx_sheet = sheet
self._keywords = keywords
self.__merged_cells = {}
self.max_row = 0
Expand All @@ -84,8 +87,8 @@ def row_iterator(self):
"""
skip hidden rows
"""
for row_index, row in enumerate(self._native_sheet.rows, 1):
if self._native_sheet.row_dimensions[row_index].hidden is False:
for row_index, row in enumerate(self.xlsx_sheet.rows, 1):
if self.xlsx_sheet.row_dimensions[row_index].hidden is False:
yield (row, row_index)
if self.max_row > self.__sheet_max_row:
for i in range(self.__sheet_max_row, self.max_row):
Expand All @@ -99,7 +102,7 @@ def column_iterator(self, row_struct):
row, row_index = row_struct
for column_index, cell in enumerate(row, 1):
letter = openpyxl.utils.get_column_letter(column_index)
if self._native_sheet.column_dimensions[letter].hidden is False:
if self.xlsx_sheet.column_dimensions[letter].hidden is False:
if cell:
value = cell.value
else:
Expand All @@ -125,7 +128,7 @@ def _merged_cells(self, row, column, value):
return ret


class XLSXBook(object):
class XLSXBook(IReader):
"""
Open xlsx as read only mode
"""
Expand All @@ -146,7 +149,7 @@ def __init__(
self._load_the_excel_file(file_alike_object)

def read_sheet(self, sheet_index):
native_sheet = self.content_array[sheet_index].sheet
native_sheet = self.content_array[sheet_index].payload
if self.skip_hidden_row_and_column or self.detect_merged_cells:
sheet = SlowSheet(native_sheet, **self.keywords)
else:
Expand All @@ -155,16 +158,16 @@ def read_sheet(self, sheet_index):

def read_all(self):
result = OrderedDict()
for index, sheet in enumerate(self._native_book):
for index, sheet in enumerate(self.xlsx_book):
if self.skip_hidden_sheets and sheet.sheet_state == "hidden":
continue
sheet = self.read_sheet(index)
result.update({sheet.name: sheet.to_array()})
return result

def close(self):
self._native_book.close()
self._native_book = None
self.xlsx_book.close()
self.xlsx_book = None

def _load_the_excel_file(self, file_alike_object):
read_only_flag = True
Expand All @@ -173,18 +176,18 @@ def _load_the_excel_file(self, file_alike_object):
data_only_flag = True
if self.detect_merged_cells:
data_only_flag = False
self._native_book = openpyxl.load_workbook(
self.xlsx_book = openpyxl.load_workbook(
filename=file_alike_object,
data_only=data_only_flag,
read_only=read_only_flag,
)
self.content_array = []
for sheet_name, sheet in zip(
self._native_book.sheetnames, self._native_book
self.xlsx_book.sheetnames, self.xlsx_book
):
if self.skip_hidden_sheets and sheet.sheet_state == "hidden":
continue
self.content_array.append(NameObject(sheet_name, sheet))
self.content_array.append(NamedContent(sheet_name, sheet))


class XLSXBookInContent(XLSXBook):
Expand All @@ -195,9 +198,3 @@ class XLSXBookInContent(XLSXBook):
def __init__(self, file_content, file_type, **keywords):
io = BytesIO(file_content)
super().__init__(io, file_type, **keywords)


class NameObject(object):
def __init__(self, name, sheet):
self.name = name
self.sheet = sheet
13 changes: 4 additions & 9 deletions pyexcel_xlsx/xlsxw.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,23 @@
Write xlsx file format using openpyxl
:copyright: (c) 2015-2019 by Onni Software Ltd & its contributors
:copyright: (c) 2015-2020 by Onni Software Ltd & its contributors
:license: New BSD License
"""
import openpyxl
from pyexcel_io import constants
from pyexcel_io.plugin_api.abstract_sheet import ISheetWriter
from pyexcel_io.plugin_api.abstract_writer import IWriter
from pyexcel_io.plugin_api import IWriter, ISheetWriter


class XLSXSheetWriter(ISheetWriter):
"""
Write data into xlsx sheet
"""

def __init__(self, xlsx_book, xlsx_sheet, sheet_name, **keywords):
def __init__(self, xlsx_sheet, sheet_name):
if sheet_name is None:
sheet_name = constants.DEFAULT_SHEET_NAME
self._xlsx_book = xlsx_book
self._xlsx_sheet = xlsx_sheet
self._keywords = keywords
self._xlsx_sheet.title = sheet_name

def write_row(self, array):
Expand All @@ -46,9 +43,7 @@ def __init__(self, file_alike_object, _, **keywords):
self._native_book = openpyxl.Workbook(write_only=True)

def create_sheet(self, name):
return XLSXSheetWriter(
self._native_book, self._native_book.create_sheet(), name
)
return XLSXSheetWriter(self._native_book.create_sheet(), name)

def close(self):
"""
Expand Down
6 changes: 2 additions & 4 deletions tests/test_writer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os

from base import PyexcelWriterBase, PyexcelHatWriterBase
from pyexcel_xlsx.xlsxr import XLSXBook as Reader
from pyexcel_xlsx import get_data
from pyexcel_xlsx.xlsxw import XLSXWriter as Writer


Expand All @@ -16,12 +16,10 @@ def test_write_book(self):
writer = Writer(self.testfile, "xlsx")
writer.write(self.content)
writer.close()
reader = Reader(self.testfile, "xlsx")
content = reader.read_all()
content = get_data(self.testfile)
for key in content.keys():
content[key] = list(content[key])
assert content == self.content
reader.close()

def tearDown(self):
if os.path.exists(self.testfile):
Expand Down

0 comments on commit a63a039

Please sign in to comment.