Skip to content

Commit

Permalink
format: fix identation and alignment property mismatch
Browse files Browse the repository at this point in the history
Fix issue where a horizontal alignment format was ignored if the
indentation was also set.

Issue: #1041
  • Loading branch information
jmcnamara committed Feb 3, 2024
1 parent 53d5086 commit e53e190
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 5 deletions.
12 changes: 12 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@



Release 3.2.0 - February XX 2023
--------------------------------

* Fix issue where a horizontal alignment format was ignored if indentation was
also set.

:issue:`1041`.


Release 3.1.9 - October 19 2023
-------------------------------

Expand Down
13 changes: 8 additions & 5 deletions xlsxwriter/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -784,14 +784,17 @@ def _get_align_properties(self):
else:
return changed, align

# Indent is only allowed for horizontal left, right and distributed.
# If it is defined for any other alignment or no alignment has
# been set then default to left alignment.
# Indent is only allowed for some alignment properties. If it is
# defined for any other alignment or no alignment has been set then
# default to left alignment.
if (
self.indent
and self.text_h_align != 1
and self.text_h_align != 3
and self.text_h_align != 7
and self.text_v_align != 1
and self.text_v_align != 3
and self.text_v_align != 5
):
self.text_h_align = 1

Expand Down Expand Up @@ -840,10 +843,10 @@ def _get_align_properties(self):
if self.text_v_align == 5:
align.append(("vertical", "distributed"))

if self.indent:
align.append(("indent", self.indent))
if self.rotation:
align.append(("textRotation", self.rotation))
if self.indent:
align.append(("indent", self.indent))

if self.text_wrap:
align.append(("wrapText", 1))
Expand Down
39 changes: 39 additions & 0 deletions xlsxwriter/test/comparison/test_format24.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
###############################################################################
#
# Tests for XlsxWriter.
#
# SPDX-License-Identifier: BSD-2-Clause
# Copyright (c), 2013-2024, John McNamara, [email protected]
#

from ..excel_comparison_test import ExcelComparisonTest
from ...workbook import Workbook


class TestCompareXLSXFiles(ExcelComparisonTest):
"""
Test file created by XlsxWriter against a file created by Excel.
"""

def setUp(self):
self.set_filename("format24.xlsx")

def test_create_file(self):
"""Test the creation of a simple XlsxWriter file with automatic color."""

workbook = Workbook(self.got_filename)

worksheet = workbook.add_worksheet()

format1 = workbook.add_format(
{"rotation": 270, "indent": 1, "align": "center", "valign": "top"}
)

worksheet.set_row(0, 75)

worksheet.write(0, 0, "ABCD", format1)

workbook.close()

self.assertExcelEqual()
Binary file not shown.

0 comments on commit e53e190

Please sign in to comment.