diff --git a/excel_import_export/README.rst b/excel_import_export/README.rst index 373e958bbc1..1d7c9048b2c 100644 --- a/excel_import_export/README.rst +++ b/excel_import_export/README.rst @@ -1,7 +1,3 @@ -.. image:: https://odoo-community.org/readme-banner-image - :target: https://odoo-community.org/get-involved?utm_source=readme - :alt: Odoo Community Association - ========================== Excel Import/Export/Report ========================== @@ -17,7 +13,7 @@ Excel Import/Export/Report .. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png :target: https://odoo-community.org/page/development-status :alt: Beta -.. |badge2| image:: https://img.shields.io/badge/license-AGPL--3-blue.png +.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html :alt: License: AGPL-3 .. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fserver--tools-lightgray.png?logo=github @@ -230,6 +226,9 @@ Contributors - Kitti Upariphutthiphong. (http://ecosoft.co.th) - Saran Lim. (http://ecosoft.co.th) - Do Anh Duy +- `Heliconia Solutions Pvt. Ltd. `__ + + - Bhavesh Heliconia Maintainers ----------- diff --git a/excel_import_export/models/common.py b/excel_import_export/models/common.py index e5462cf8504..16cefe96fbe 100644 --- a/excel_import_export/models/common.py +++ b/excel_import_export/models/common.py @@ -7,12 +7,20 @@ import string import uuid from ast import literal_eval +from datetime import date from datetime import datetime as dt -from io import StringIO +from io import BytesIO, StringIO import xlrd from dateutil.parser import parse +from odoo.tools.parse_version import parse_version + +try: + from openpyxl import load_workbook +except ImportError: + load_workbook = None + from odoo import _ from odoo.exceptions import ValidationError @@ -223,8 +231,34 @@ def str_to_number(input_val): def csv_from_excel(excel_content, delimiter, quote): - wb = xlrd.open_workbook(file_contents=excel_content) - sh = wb.sheet_by_index(0) + try: + wb = xlrd.open_workbook(file_contents=excel_content) + sh = wb.sheet_by_index(0) + rows_iter = (sh.row_values(rownum) for rownum in range(sh.nrows)) + except ( + xlrd.XLRDError, + Exception, + ): # Fallback to openpyxl if xlrd fails (e.g. xlsx in xlrd>=2.0) + if ( + xlrd + and parse_version(xlrd.__VERSION__) >= parse_version("2.0") + and load_workbook + ): + # Try openpyxl + try: + wb = load_workbook( + StringIO(excel_content) + if isinstance(excel_content, str) + else BytesIO(excel_content or b""), + data_only=True, + ) + sh = wb.worksheets[0] + rows_iter = sh.iter_rows(values_only=True) + except Exception: + raise + else: + raise + content = StringIO() quoting = csv.QUOTE_ALL if not quote: @@ -232,10 +266,22 @@ def csv_from_excel(excel_content, delimiter, quote): if delimiter == " " and quoting == csv.QUOTE_NONE: quoting = csv.QUOTE_MINIMAL wr = csv.writer(content, delimiter=delimiter, quoting=quoting) - for rownum in range(sh.nrows): + + for row_values in rows_iter: row = [] - for x in sh.row_values(rownum): - if quoting == csv.QUOTE_NONE and delimiter in x: + for x in row_values: + if isinstance(x, dt | date): + is_datetime = False + if isinstance(x, dt) and ( + x.hour or x.minute or x.second or x.microsecond + ): + is_datetime = True + x = ( + x.strftime("%Y-%m-%d %H:%M:%S") + if is_datetime + else x.strftime("%Y-%m-%d") + ) + if quoting == csv.QUOTE_NONE and delimiter in str(x): raise ValidationError( _( "Template with CSV Quoting = False, data must not " @@ -267,19 +313,19 @@ def _get_cell_value(cell, field_type=False): if not know, just get value as is""" value = False datemode = 0 # From book.datemode, but we fix it for simplicity - if field_type in ["date", "datetime"]: - ctype = xlrd.sheet.ctype_text.get(cell.ctype, "unknown type") - if ctype in ("xldate", "number"): - is_datetime = cell.value % 1 != 0.0 + if field_type in ("date", "datetime"): + if hasattr(cell, "ctype") and cell.ctype == xlrd.XL_CELL_DATE: time_tuple = xlrd.xldate_as_tuple(cell.value, datemode) - date = dt(*time_tuple) - value = ( - date.strftime("%Y-%m-%d %H:%M:%S") - if is_datetime - else date.strftime("%Y-%m-%d") - ) + value = dt(*time_tuple) else: value = cell.value + + if isinstance(value, dt | date): + if field_type == "date": + value = value.strftime("%Y-%m-%d") + else: + value = value.strftime("%Y-%m-%d %H:%M:%S") + elif field_type in ["integer", "float"]: value_str = str(cell.value).strip().replace(",", "") if len(value_str) == 0: @@ -297,8 +343,8 @@ def _get_cell_value(cell, field_type=False): value = str(cell.value) else: value = cell.value - else: # text, char - value = cell.value + else: # text, char, or unknown field_type + value = cell.value if hasattr(cell, "value") else cell # If string, cleanup if isinstance(value, str): if value[-2:] == ".0": diff --git a/excel_import_export/models/xlsx_import.py b/excel_import_export/models/xlsx_import.py index 1892396c7f7..5754ac6fdde 100644 --- a/excel_import_export/models/xlsx_import.py +++ b/excel_import_export/models/xlsx_import.py @@ -206,7 +206,7 @@ def _process_worksheet( row, col = co.pos2idx(rc) if is_xlsx: # openpyxl # openpyxl uses 1-based indexing - cell_value = st.cell(row=row + 1, column=col + 1).value + cell_value = st.cell(row=row + 1, column=col + 1) else: # xlrd cell_value = st.cell(row, col) value = co._get_cell_value(cell_value, field_type=field_type) @@ -249,7 +249,7 @@ def _import_record_data(self, import_file, record, data_dict): out_st.write(0, 0, "id") out_st.write(1, 0, xml_id) header_fields = ["id"] - is_xlsx = self._is_xlsx_file() + is_xlsx = self._is_xlsx_file(decoded_data) wb = self._load_workbook(decoded_data, is_xlsx) # Process on all worksheets self._process_worksheet( @@ -279,10 +279,15 @@ def _generate_xml_id(self, record): or "{}.{}".format("__excel_import_export__", uuid.uuid4()) ) - def _is_xlsx_file(self): - if xlrd and parse_version(xlrd.__VERSION__) < parse_version("2.0"): + def _is_xlsx_file(self, content): + if not content: return False - return True + # Check for Zip signature (PK..). Used by .xlsx, .xlsm + if content[:4] == b"\x50\x4b\x03\x04": + if xlrd and parse_version(xlrd.__VERSION__) < parse_version("2.0"): + return False + return True + return False def _load_workbook(self, decoded_data, is_xlsx): if not is_xlsx: diff --git a/excel_import_export/readme/CONTRIBUTORS.md b/excel_import_export/readme/CONTRIBUTORS.md index f5c065bfe39..92d02d16e20 100644 --- a/excel_import_export/readme/CONTRIBUTORS.md +++ b/excel_import_export/readme/CONTRIBUTORS.md @@ -2,3 +2,5 @@ () - Saran Lim. \<\> () - Do Anh Duy \<\> +- [Heliconia Solutions Pvt. Ltd.](https://www.heliconia.io) + - Bhavesh Heliconia diff --git a/excel_import_export/static/description/index.html b/excel_import_export/static/description/index.html index 80bf4d3b4be..765bf7fc9b3 100644 --- a/excel_import_export/static/description/index.html +++ b/excel_import_export/static/description/index.html @@ -3,7 +3,7 @@ -README.rst +Excel Import/Export/Report -
+
+

Excel Import/Export/Report

- - -Odoo Community Association - -
-

Excel Import/Export/Report

-

Beta License: AGPL-3 OCA/server-tools Translate me on Weblate Try me on Runboat

+

Beta License: AGPL-3 OCA/server-tools Translate me on Weblate Try me on Runboat

The module provide pre-built functions and wizards for developer to build excel import / export / report with ease.

Without having to code to create excel file, developer do,

@@ -409,22 +404,22 @@

Excel Import/Export/Report

-

Installation

+

Installation

To install this module, you need to install following python library, xlrd, xlwt, openpyxl.

Then, simply install excel_import_export.

For demo, install excel_import_export_demo

-

Configuration

+

Configuration

If you have existing templates from the version 16.0.1.2.0 or earlier, you need to click ‘REMOVE EXPORT ACTION’ and then click ‘ADD EXPORT ACTION’ in these templates for export actions to work as expected.

-

Usage

+

Usage

-

Concepts

+

Concepts

This module contain pre-defined function and wizards to make exporting, importing and reporting easy.

At the heart of this module, there are 2 main methods

@@ -442,7 +437,7 @@

Concepts

example use cases, please install excel_import_export_demo

-

Use Cases

+

Use Cases

Use Case 1: Export/Import Excel on existing document

This add export/import action menus in existing document (example - excel_import_export_demo/import_export_sale_order)

@@ -511,7 +506,7 @@

Use Cases

-

Easy Reporting Option

+

Easy Reporting Option

Technically, this option is the same as “Create Excel Report” use case. But instead of having to write XML / Python code like normally do, this option allow user to create a report based on a model or view, all by @@ -541,14 +536,14 @@

Easy Reporting Option

-

Known issues / Roadmap

+

Known issues / Roadmap

  • Module extension e.g., excel_import_export_async, that add ability to execute as async process.
-

Bug Tracker

+

Bug Tracker

Bugs are tracked on GitHub Issues. In case of trouble, please check there if your issue has already been reported. If you spotted it first, help us to smash it by providing a detailed and welcomed @@ -556,23 +551,27 @@

Bug Tracker

Do not contact contributors directly about support or help with technical issues.

-