Skip to content

Commit

Permalink
Merge pull request #119 from mojaie/master
Browse files Browse the repository at this point in the history
Add positioning option to insert_image #117
  • Loading branch information
jmcnamara committed Apr 30, 2014
2 parents e1059aa + 393d657 commit 9651cec
Show file tree
Hide file tree
Showing 18 changed files with 562 additions and 13 deletions.
25 changes: 19 additions & 6 deletions dev/docs/source/worksheet.rst
Expand Up @@ -966,12 +966,13 @@ position and scale the image. The available parameters with their default
values are::

{
'x_offset': 0,
'y_offset': 0,
'x_scale': 1,
'y_scale': 1,
'url': None,
'tip': None,
'x_offset': 0,
'y_offset': 0,
'x_scale': 1,
'y_scale': 1,
'url': None,
'tip': None,
'positioning': None
}

The offset values are in pixels::
Expand All @@ -994,6 +995,18 @@ parameter gives an option mouseover tooltip for images with hyperlinks::

See also :func:`write_url` for details on supported URIs.

The ``positioning`` parameter can be used to control the object positioning
of the image::

worksheet.insert_image('B3', 'python.png', {'positioning': 1})

Where ``positioning`` has the following allowable values:

1. Move and size with cells.
2. Move but don't size with cells (the default).
3. Don't move or size with cells.


.. Note::
The scaling of a image may be affected if is crosses a row that has its
default height changed due to a font that is larger than the default font
Expand Down
11 changes: 9 additions & 2 deletions xlsxwriter/drawing.py
Expand Up @@ -86,12 +86,14 @@ def _add_drawing_object(self, drawing_object):
'description': drawing_object[13],
'shape': drawing_object[14],
'url': None,
'tip': None
'tip': None,
'anchor': None
}

if len(drawing_object) > 15:
obj['url'] = drawing_object[15]
obj['tip'] = drawing_object[16]
obj['anchor'] = drawing_object[17]

self.drawings.append(obj)

Expand Down Expand Up @@ -128,7 +130,12 @@ def _write_two_cell_anchor(self, index, drawing):

# Add attribute for images.
if drawing['anchor_type'] == 2:
attributes.append(('editAs', 'oneCell'))
if drawing['anchor'] == 3:
attributes.append(('editAs', 'absolute'))
elif drawing['anchor'] == 1:
pass
else:
attributes.append(('editAs', 'oneCell'))

# Add editAs attribute for shapes.
if shape and shape.editAs:
Expand Down
88 changes: 88 additions & 0 deletions xlsxwriter/test/comparison/test_image_anchor01.py
@@ -0,0 +1,88 @@
###############################################################################
#
# Tests for XlsxWriter.
#
# Copyright (c), 2013-2014, John McNamara, jmcnamara@cpan.org
#

import unittest
import os
from ...workbook import Workbook
from ..helperfunctions import _compare_xlsx_files


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

def setUp(self):
self.maxDiff = None

filename = 'image_anchor01.xlsx'

test_dir = 'xlsxwriter/test/comparison/'
self.image_dir = test_dir + 'images/'
self.got_filename = test_dir + '_test_' + filename
self.exp_filename = test_dir + 'xlsx_files/' + filename

self.ignore_files = []
self.ignore_elements = {}

def test_create_file(self):
"""Test the creation of a simple XlsxWriter file with image(s)."""
filename = self.got_filename

####################################################

workbook = Workbook(filename)

worksheet = workbook.add_worksheet()

worksheet.insert_image(
'E9', self.image_dir + 'red.png', {'positioning': 3})

workbook.close()

####################################################

got, exp = _compare_xlsx_files(self.got_filename,
self.exp_filename,
self.ignore_files,
self.ignore_elements)

self.assertEqual(got, exp)

def test_create_file_in_memory(self):
"""Test the creation of a simple XlsxWriter file with image(s)."""
filename = self.got_filename

####################################################

workbook = Workbook(filename, {'in_memory': True})

worksheet = workbook.add_worksheet()

worksheet.insert_image(
'E9', self.image_dir + 'red.png', {'positioning': 3})

workbook.close()

####################################################

got, exp = _compare_xlsx_files(self.got_filename,
self.exp_filename,
self.ignore_files,
self.ignore_elements)

self.assertEqual(got, exp)

def tearDown(self):
# Cleanup.
if os.path.exists(self.got_filename):
os.remove(self.got_filename)


if __name__ == '__main__':
unittest.main()
88 changes: 88 additions & 0 deletions xlsxwriter/test/comparison/test_image_anchor02.py
@@ -0,0 +1,88 @@
###############################################################################
#
# Tests for XlsxWriter.
#
# Copyright (c), 2013-2014, John McNamara, jmcnamara@cpan.org
#

import unittest
import os
from ...workbook import Workbook
from ..helperfunctions import _compare_xlsx_files


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

def setUp(self):
self.maxDiff = None

filename = 'image_anchor02.xlsx'

test_dir = 'xlsxwriter/test/comparison/'
self.image_dir = test_dir + 'images/'
self.got_filename = test_dir + '_test_' + filename
self.exp_filename = test_dir + 'xlsx_files/' + filename

self.ignore_files = []
self.ignore_elements = {}

def test_create_file(self):
"""Test the creation of a simple XlsxWriter file with image(s)."""
filename = self.got_filename

####################################################

workbook = Workbook(filename)

worksheet = workbook.add_worksheet()

worksheet.insert_image(
'E9', self.image_dir + 'red.png', {'positioning': 2})

workbook.close()

####################################################

got, exp = _compare_xlsx_files(self.got_filename,
self.exp_filename,
self.ignore_files,
self.ignore_elements)

self.assertEqual(got, exp)

def test_create_file_in_memory(self):
"""Test the creation of a simple XlsxWriter file with image(s)."""
filename = self.got_filename

####################################################

workbook = Workbook(filename, {'in_memory': True})

worksheet = workbook.add_worksheet()

worksheet.insert_image(
'E9', self.image_dir + 'red.png', {'positioning': 2})

workbook.close()

####################################################

got, exp = _compare_xlsx_files(self.got_filename,
self.exp_filename,
self.ignore_files,
self.ignore_elements)

self.assertEqual(got, exp)

def tearDown(self):
# Cleanup.
if os.path.exists(self.got_filename):
os.remove(self.got_filename)


if __name__ == '__main__':
unittest.main()
88 changes: 88 additions & 0 deletions xlsxwriter/test/comparison/test_image_anchor03.py
@@ -0,0 +1,88 @@
###############################################################################
#
# Tests for XlsxWriter.
#
# Copyright (c), 2013-2014, John McNamara, jmcnamara@cpan.org
#

import unittest
import os
from ...workbook import Workbook
from ..helperfunctions import _compare_xlsx_files


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

def setUp(self):
self.maxDiff = None

filename = 'image_anchor03.xlsx'

test_dir = 'xlsxwriter/test/comparison/'
self.image_dir = test_dir + 'images/'
self.got_filename = test_dir + '_test_' + filename
self.exp_filename = test_dir + 'xlsx_files/' + filename

self.ignore_files = []
self.ignore_elements = {}

def test_create_file(self):
"""Test the creation of a simple XlsxWriter file with image(s)."""
filename = self.got_filename

####################################################

workbook = Workbook(filename)

worksheet = workbook.add_worksheet()

worksheet.insert_image(
'E9', self.image_dir + 'red.png', {'positioning': 1})

workbook.close()

####################################################

got, exp = _compare_xlsx_files(self.got_filename,
self.exp_filename,
self.ignore_files,
self.ignore_elements)

self.assertEqual(got, exp)

def test_create_file_in_memory(self):
"""Test the creation of a simple XlsxWriter file with image(s)."""
filename = self.got_filename

####################################################

workbook = Workbook(filename, {'in_memory': True})

worksheet = workbook.add_worksheet()

worksheet.insert_image(
'E9', self.image_dir + 'red.png', {'positioning': 1})

workbook.close()

####################################################

got, exp = _compare_xlsx_files(self.got_filename,
self.exp_filename,
self.ignore_files,
self.ignore_elements)

self.assertEqual(got, exp)

def tearDown(self):
# Cleanup.
if os.path.exists(self.got_filename):
os.remove(self.got_filename)


if __name__ == '__main__':
unittest.main()

0 comments on commit 9651cec

Please sign in to comment.