Skip to content

Commit

Permalink
Added text alignment for textboxes.
Browse files Browse the repository at this point in the history
Added text alignment for textboxes. The existing options allowed
the text area to be aligned but didn't offer control over the
text within that area.

Issue #684
  • Loading branch information
jmcnamara committed Sep 26, 2020
1 parent b4be68d commit 0ce85f1
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 4 deletions.
Binary file added dev/docs/source/_images/textbox50.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 17 additions & 4 deletions dev/docs/source/working_with_textboxes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -458,15 +458,28 @@ The alignment properties that can be set in Excel for a textbox are::
{'align': {'horizontal': 'left'}} # Default
{'align': {'horizontal': 'center'}}

Note, Excel doesn't support right text alignment for the entire textbox. It
does support it for text within the textbox but that currently isn't supported
by XlsxWriter, see the next section.
{'align': {'text': 'left'}} # Default
{'align': {'text': 'center'}}
{'align': {'text': 'right'}}

The ``vertical`` and ``horizontal`` alignments set the layout for the text
area within the textbox. The ``text`` alignment sets the layout for the text
within that text area::

worksheet.insert_textbox('H2',
'Long text line that wraps and is centered',
{'align': {'vertical': 'middle',
'horizontal': 'center',
'text': 'center'}})

.. image:: _images/textbox50.png

The default textbox alignment is::

worksheet.insert_textbox('B2', 'Default alignment',
{'align': {'vertical': 'top',
'horizontal': 'left'}})
'horizontal': 'left',
'text': 'left'}})

# Same as this:
worksheet.insert_textbox('B2', 'Default alignment')
Expand Down
10 changes: 10 additions & 0 deletions examples/textbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,16 @@
worksheet.insert_textbox(row, col, text, options)
row += 10

# Example
text = 'Alignment: long text line that wraps and is centered'
options = {
'align': {'vertical': 'middle',
'horizontal': 'center',
'text': 'center'},
}
worksheet.insert_textbox(row, col, text, options)
row += 10

# Example
text = 'Font properties: bold'
options = {
Expand Down
7 changes: 7 additions & 0 deletions xlsxwriter/drawing.py
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,13 @@ def _write_tx_body(self, col_absolute, row_absolute, width, height, shape):
'a:endParaRPr')
self._xml_end_tag('a:p')
continue
elif 'text' in shape.align:
if shape.align['text'] == 'left':
self._xml_empty_tag('a:pPr', [('algn', 'l')])
if shape.align['text'] == 'center':
self._xml_empty_tag('a:pPr', [('algn', 'ctr')])
if shape.align['text'] == 'right':
self._xml_empty_tag('a:pPr', [('algn', 'r')])

self._xml_start_tag('a:r')

Expand Down
48 changes: 48 additions & 0 deletions xlsxwriter/test/comparison/test_textbox41.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
###############################################################################
#
# Tests for XlsxWriter.
#
# Copyright (c), 2013-2020, John McNamara, jmcnamara@cpan.org
#

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('textbox41.xlsx')

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

workbook = Workbook(self.got_filename)

worksheet = workbook.add_worksheet()

worksheet.insert_textbox('E9', 'This is some text')

worksheet.insert_textbox('E19', 'This is some text',
{'align': {'vertical': 'top',
'horizontal': 'center',
'text': 'left'}})

worksheet.insert_textbox('E29', 'This is some text',
{'align': {'vertical': 'top',
'horizontal': 'center',
'text': 'center'}})

worksheet.insert_textbox('E39', 'This is some text',
{'align': {'vertical': 'top',
'horizontal': 'center',
'text': 'right'}})

workbook.close()

self.assertExcelEqual()
Binary file not shown.

0 comments on commit 0ce85f1

Please sign in to comment.