err*_*ata 5 python excel python-3.x openpyxl
我正在使用openpyxl打开一个 .xlsx 文件,更新其中的一些值并将其另存为不同的 .xlsx 文件。我正在尝试添加一个带有新行的页脚:
# 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')
Run Code Online (Sandbox Code Playgroud)
但是当我打开新创建的文件并查看页脚时,\n会以一种奇怪的方式被替换:
Some text^lMore text^pEven more
Run Code Online (Sandbox Code Playgroud)
我还注意到,如果我尝试在 libreoffice 的帮助下将其转换为 PDF,例如像这样:
os.system('libreoffice --headless --invisible --convert-to pdf --outdir /path/on/disk new_file.xlsx')
Run Code Online (Sandbox Code Playgroud)
生成的 PDF 再次将其呈现为不同的内容:
Some text_x000D_More text_x000D_Even more
Run Code Online (Sandbox Code Playgroud)
如何在页脚中正确生成新行?
(可能值得一提的是,我在 Ubuntu 14.04 上使用带有 Python 3.4 的 openpyxl 2.3.3。LibreOffice 的版本是 5.0.5.2)