11# Copyright 2017 Creu Blanca
2+ # Copyright 2025 XCG SAS
23# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
34
5+ import io
46import logging
7+ import unittest
58
6- from odoo .tests import common
9+ from odoo .tests import can_import , common
710
811_logger = logging .getLogger (__name__ )
912
10- try :
11- from xlrd import open_workbook
12- except ImportError :
13- _logger .debug ("Can not import xlrd`." )
13+ try : # pragma: no cover
14+ from openpyxl import load_workbook
15+ except ImportError : # pragma: no cover
16+ _logger .debug ("Can not import openpyxl." )
17+ load_workbook = None
18+ try :
19+ from xlrd import open_workbook
20+ except ImportError :
21+ _logger .debug ("Can not import xlrd`." )
22+ open_workbook = None
1423
1524
1625class TestReport (common .TransactionCase ):
@@ -24,13 +33,22 @@ def setUp(self):
2433 self .report = self .report_object ._get_report_from_name (self .report_name )
2534 self .docs = self .env ["res.company" ].search ([], limit = 1 ).partner_id
2635
36+ @unittest .skipUnless (
37+ can_import ("xlrd.xlsx" ) or can_import ("openpyxl" ), "XLRD/XLSX not available"
38+ )
2739 def test_report (self ):
2840 report = self .report
2941 self .assertEqual (report .report_type , "xlsx" )
3042 rep = self .report_object ._render (self .report_name , self .docs .ids , {})
31- wb = open_workbook (file_contents = rep [0 ])
32- sheet = wb .sheet_by_index (0 )
33- self .assertEqual (sheet .cell (0 , 0 ).value , self .docs .name )
43+ if load_workbook : # pragma: no cover
44+ wb = load_workbook (io .BytesIO (rep [0 ]), read_only = True )
45+ sheet = wb [wb .sheetnames [0 ]]
46+ cell_0_0 = sheet .cell (1 , 1 )
47+ elif open_workbook : # pragma: no cover
48+ wb = open_workbook (file_contents = rep [0 ])
49+ sheet = wb .sheet_by_index (0 )
50+ cell_0_0 = sheet .cell (0 , 0 )
51+ self .assertEqual (cell_0_0 .value , self .docs .name )
3452
3553 def test_save_attachment (self ):
3654 self .report .attachment = 'object.name + ".xlsx"'
0 commit comments