Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rendering out new line properly in openpyxl generated XLSX file

I'm using openpyxl to open an .xlsx file, update some values in it and save it as a different .xlsx file. I am trying to add a footer with new lines in it:

# example code
wb = openpyxl.load_workbook('file.xlsx')
sheet = wb.get_sheet_by_name('Sheet1')
sheet.header_footer.left_footer.font_size = 7
sheet.header_footer.left_footer.text = '&BSome text&B\nMore text\nEven more'
sheet.header_footer.right_footer.font_size = 7
sheet.header_footer.right_footer.text = 'Page &P of &N'
wb.save('new_file.xlsx')

But when I open newly created file and look at the footer, \n gets replaced in a weird way:

Some text^lMore text^pEven more

I have also noticed that if I try to convert it to PDF with a help of libreoffice, e.g. like this:

os.system('libreoffice --headless --invisible --convert-to pdf --outdir /path/on/disk new_file.xlsx')

the generated PDF renders it out to something different again:

Some text_x000D_More text_x000D_Even more

How can I properly generate new line in the footer?
(Might be worth of mentioning that I'm using openpyxl 2.3.3 with Python 3.4 on Ubuntu 14.04. Version of LibreOffice is 5.0.5.2)

like image 491
errata Avatar asked Feb 17 '16 14:02

errata


People also ask

Does openpyxl work with Xlsx?

Openpyxl is a Python library for reading and writing Excel (with extension xlsx/xlsm/xltx/xltm) files. The openpyxl module allows Python program to read and modify Excel files.


1 Answers

Excel's headers and footers use a weird control code format where \n is replaced by x000D in XML. The footer is correct but LibreOffice is not rendering it correctly.

like image 185
Charlie Clark Avatar answered Sep 18 '22 01:09

Charlie Clark