4

I've needed to create a custom invoice using Python XlsxWriter, where I need to create a custom header with a company logo. The resulting workbook must be opened using LibreOffice, not Excel.

Using the example from here. My code is:

#!/usr/bin/env python

try:
    import xlsxwriter
except ImportError as e:
    print e
    exit()


workbook = xlsxwriter.Workbook('tmp.xlsx')

preview = 'Select Print Preview to see the header and footer'

# Insert a header image.
#
worksheet1 = workbook.add_worksheet('Simple')
header1 = '&L&G'
footer1 = '&LHere is some left aligned text.'

worksheet1.set_header(header1, {'image_left': 'logo.png'})
worksheet1.set_footer(footer1)
worksheet1.set_margins(top=1.3)

# Insert an image.
worksheet1.write('A2', 'Insert an image in a cell:')
worksheet1.insert_image('B2', 'logo.png')

worksheet1.set_column('A:A', 50)
worksheet1.write('A1', preview)
workbook.close()

While my logo can be inserted in B2 cell, my image is not to be found in the header, when opened with LibreOffice. The image appears as it should in Excel.

Community
  • 1
  • 1
Gavin Yap
  • 642
  • 12
  • 26

2 Answers2

0

Your example does work. Here is the output I get when I run it with the latest version of the module and Excel 2013:

enter image description here

Perhaps you have an old version of XlsxWriter. Check your version as follows:

python -c "import xlsxwriter; print(xlsxwriter.__version__)"

It should be at least 0.6.0 and the current version is 0.9.2.

John Y
  • 14,123
  • 2
  • 48
  • 72
jmcnamara
  • 38,196
  • 6
  • 90
  • 108
  • May I check with you if you are using excel to open or you are using openoffice to open the written file? I tried in libreoffice and it does not show. and yes, I'm using 0.9.2 gavinyap@gavin-ubuntu  ~/Development/excel  python -c 'import xlsxwriter; print(xlsxwriter.__version__)' 0.9.2 – Gavin Yap Jul 12 '16 at 08:45
  • I am using Excel 2013. Also, I am the author of XlsxWriter and there are a number of tests in the repo that compare the output with header image files created by Excel 2007 and they all pass. So it does work. :-) – jmcnamara Jul 12 '16 at 08:57
  • Sorry but I'm unsure if it is due to LibreOffice that is not displaying. – Gavin Yap Jul 12 '16 at 09:08
  • If it works in Excel, the application that the module is targeting, but doesn't work in LibreOffice then I would think that the issue is with LibreOffice. No? – jmcnamara Jul 12 '16 at 09:28
  • Yes. I agree with you, it works in excel. And the problem lies in LibreOffice which I had state in my answer. But unfortunate to me, is that I need it to work in libreoffice(or similar) in order to convert to PDF using their cmd line tools. – Gavin Yap Jul 12 '16 at 09:54
0

Edited

The xlsx with logo header works perfectly in Excel.

It is not displaying in LibreOffice(5.1.4.2 10m0(Build:2)),which is could potentially be a bug in LibreOffice/OpenOffice.

Gavin Yap
  • 642
  • 12
  • 26
  • I just want to be clear since people will find this via google. It may not work in Libreoffice issue but that is not an XlsxWriter issue. XlsxWriter contains 9 test cases like [this one](https://github.com/jmcnamara/XlsxWriter/blob/master/xlsxwriter/test/comparison/test_header_image01.py) that compare the output of XlsxWriter, XML element by XML element, with files created in Excel. In all cases the output is 100% the same (apart from metadata). These tests are passing on a [range of Pythons](https://travis-ci.org/jmcnamara/XlsxWriter). – jmcnamara Jul 12 '16 at 09:36
  • You can verify this by creating a file in Excel with a header image and trying it in LibreOffice. – jmcnamara Jul 12 '16 at 09:43
  • Yes. it works perfectly for Excel. And probably only for targeted users that are using Excel application. – Gavin Yap Jul 12 '16 at 09:52