Skip to content

Commit

Permalink
Initial working header/footer images.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcnamara committed Oct 5, 2014
1 parent 29b3aba commit 1b65ee1
Show file tree
Hide file tree
Showing 29 changed files with 917 additions and 59 deletions.
Binary file removed chart_doughnut.xlsx
Binary file not shown.
Binary file removed chart_pie.xlsx
Binary file not shown.
56 changes: 38 additions & 18 deletions examples/headers_footers.py
Expand Up @@ -26,6 +26,8 @@
# &X Superscript
# &Y Subscript
#
# &G Images Image placeholder
#
# && Miscellaneous Literal ampersand &
#
# See the main XlsxWriter documentation for more information.
Expand All @@ -51,58 +53,76 @@
worksheet1.set_column('A:A', 50)
worksheet1.write('A1', preview)


######################################################################
#
# This is an example of some of the header/footer variables.
# Insert a header image.
#
worksheet2 = workbook.add_worksheet('Variables')
header2 = '&LPage &P of &N' + '&CFilename: &F' + '&RSheetname: &A'
footer2 = '&LCurrent date: &D' + '&RCurrent time: &T'
worksheet2 = workbook.add_worksheet('Image')
header2 = '&L&G'

worksheet2.set_header(header2)
worksheet2.set_footer(footer2)
# Adjust the page top margin to allow space for the header image.
worksheet2.set_margins(top=2.0)

worksheet2.set_header(header2, None,
{'image_left': 'python-200x80.png'})

worksheet2.set_column('A:A', 50)
worksheet2.write('A1', preview)
worksheet2.write('A21', 'Next sheet')
worksheet2.set_h_pagebreaks([20])


######################################################################
#
# This example shows how to use more than one font
# This is an example of some of the header/footer variables.
#
worksheet3 = workbook.add_worksheet('Mixed fonts')
header3 = '&C&"Courier New,Bold"Hello &"Arial,Italic"World'
footer3 = '&C&"Symbol"e&"Arial" = mc&X2'
worksheet3 = workbook.add_worksheet('Variables')
header3 = '&LPage &P of &N' + '&CFilename: &F' + '&RSheetname: &A'
footer3 = '&LCurrent date: &D' + '&RCurrent time: &T'

worksheet3.set_header(header3)
worksheet3.set_footer(footer3)

worksheet3.set_column('A:A', 50)
worksheet3.write('A1', preview)
worksheet3.write('A21', 'Next sheet')
worksheet3.set_h_pagebreaks([20])

######################################################################
#
# Example of line wrapping
# This example shows how to use more than one font
#
worksheet4 = workbook.add_worksheet('Word wrap')
header4 = "&CHeading 1\nHeading 2"
worksheet4 = workbook.add_worksheet('Mixed fonts')
header4 = '&C&"Courier New,Bold"Hello &"Arial,Italic"World'
footer4 = '&C&"Symbol"e&"Arial" = mc&X2'

worksheet4.set_header(header4)
worksheet4.set_footer(footer4)

worksheet4.set_column('A:A', 50)
worksheet4.write('A1', preview)

######################################################################
#
# Example of inserting a literal ampersand &
# Example of line wrapping
#
worksheet5 = workbook.add_worksheet('Ampersand')
header5 = '&CCuriouser && Curiouser - Attorneys at Law'
worksheet5 = workbook.add_worksheet('Word wrap')
header5 = "&CHeading 1\nHeading 2"

worksheet5.set_header(header5)

worksheet5.set_column('A:A', 50)
worksheet5.write('A1', preview)

######################################################################
#
# Example of inserting a literal ampersand &
#
worksheet6 = workbook.add_worksheet('Ampersand')
header6 = '&CCuriouser && Curiouser - Attorneys at Law'

worksheet6.set_header(header6)

worksheet6.set_column('A:A', 50)
worksheet6.write('A1', preview)

workbook.close()
Binary file added examples/python-200x80.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 40 additions & 10 deletions xlsxwriter/packager.py
Expand Up @@ -229,17 +229,31 @@ def _write_vml_files(self):
# Write the comment VML files.
index = 1
for worksheet in self.workbook.worksheets():
if not worksheet.has_vml:
if not worksheet.has_vml and not worksheet.has_header_vml:
continue
if worksheet.has_vml:
vml = Vml()
vml._set_xml_writer(self._filename('xl/drawings/vmlDrawing'
+ str(index) + '.vml'))
vml._assemble_xml_file(worksheet.vml_data_id,
worksheet.vml_shape_id,
worksheet.comments_list,
worksheet.buttons_list)
index += 1

vml = Vml()
vml._set_xml_writer(self._filename('xl/drawings/vmlDrawing'
+ str(index) + '.vml'))
vml._assemble_xml_file(worksheet.vml_data_id,
worksheet.vml_shape_id,
worksheet.comments_array,
worksheet.buttons_array)
index += 1
if worksheet.has_header_vml:
vml = Vml()

vml._set_xml_writer(self._filename('/xl/drawings/vmlDrawing'
+ str(index) + '.vml'))
vml._assemble_xml_file(worksheet.vml_header_id,
worksheet.vml_header_id * 1024,
None,
None,
worksheet.header_images_list)

self._write_vml_drawing_rels_file(worksheet, index)
index += 1

def _write_comment_files(self):
# Write the comment files.
Expand All @@ -251,7 +265,7 @@ def _write_comment_files(self):
comment = Comments()
comment._set_xml_writer(self._filename('xl/comments'
+ str(index) + '.xml'))
comment._assemble_xml_file(worksheet.comments_array)
comment._assemble_xml_file(worksheet.comments_list)
index += 1

def _write_shared_strings_file(self):
Expand Down Expand Up @@ -525,6 +539,22 @@ def _write_drawing_rels_files(self):
+ str(index) + '.xml.rels'))
rels._assemble_xml_file()

def _write_vml_drawing_rels_file(self, worksheet, index):
# Write the vmlDdrawing .rels files for worksheets with images in
# headers or footers.

# Create the drawing .rels dir.
rels = Relationships()

for drawing_data in worksheet.vml_drawing_links:
rels._add_document_relationship(*drawing_data)

# Create the .rels file such as /xl/drawings/_rels/vmlDrawing1.vml.rels.
rels._set_xml_writer(self._filename('/xl/drawings/_rels/vmlDrawing'
+ str(index)
+ '.vml.rels'))
rels._assemble_xml_file()

def _add_image_files(self):
# Write the /xl/media/image?.xml files.
workbook = self.workbook
Expand Down
43 changes: 43 additions & 0 deletions xlsxwriter/test/comparison/test_header_image01.py
@@ -0,0 +1,43 @@
###############################################################################
#
# Tests for XlsxWriter.
#
# Copyright (c), 2013-2014, John McNamara, jmcnamara@cpan.org
#

from ..excel_comparsion_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.maxDiff = None

filename = 'header_image01.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)."""

workbook = Workbook(self.got_filename)

worksheet = workbook.add_worksheet()

worksheet.set_header('&L&G', None,
{'image_left': self.image_dir + 'red.jpg'})

workbook.close()

self.assertExcelEqual()
44 changes: 44 additions & 0 deletions xlsxwriter/test/comparison/test_header_image02.py
@@ -0,0 +1,44 @@
###############################################################################
#
# Tests for XlsxWriter.
#
# Copyright (c), 2013-2014, John McNamara, jmcnamara@cpan.org
#

from ..excel_comparsion_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.maxDiff = None

filename = 'header_image02.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)."""

workbook = Workbook(self.got_filename)

worksheet = workbook.add_worksheet()

worksheet.set_header('&L&G&C&G', None,
{'image_left': self.image_dir + 'red.jpg',
'image_center': self.image_dir + 'blue.jpg'})

workbook.close()

self.assertExcelEqual()
45 changes: 45 additions & 0 deletions xlsxwriter/test/comparison/test_header_image03.py
@@ -0,0 +1,45 @@
###############################################################################
#
# Tests for XlsxWriter.
#
# Copyright (c), 2013-2014, John McNamara, jmcnamara@cpan.org
#

from ..excel_comparsion_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.maxDiff = None

filename = 'header_image03.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)."""

workbook = Workbook(self.got_filename)

worksheet = workbook.add_worksheet()

worksheet.set_header('&L&G&C&G&R&G', None,
{'image_left': self.image_dir + 'red.jpg',
'image_center': self.image_dir + 'blue.jpg',
'image_right': self.image_dir + 'yellow.jpg'})

workbook.close()

self.assertExcelEqual()
45 changes: 45 additions & 0 deletions xlsxwriter/test/comparison/test_header_image04.py
@@ -0,0 +1,45 @@
###############################################################################
#
# Tests for XlsxWriter.
#
# Copyright (c), 2013-2014, John McNamara, jmcnamara@cpan.org
#

from ..excel_comparsion_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.maxDiff = None

filename = 'header_image04.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)."""

workbook = Workbook(self.got_filename)

worksheet = workbook.add_worksheet()

worksheet.set_footer('&L&G&C&G&R&G', None,
{'image_left': self.image_dir + 'red.jpg',
'image_center': self.image_dir + 'blue.jpg',
'image_right': self.image_dir + 'yellow.jpg'})

workbook.close()

self.assertExcelEqual()

0 comments on commit 1b65ee1

Please sign in to comment.