gpt4 book ai didi

python - xlsxwriter 的 set_header 未按预期工作

转载 作者:太空宇宙 更新时间:2023-11-04 02:17:12 26 4
gpt4 key购买 nike

我正在使用 xlsxwriter 1.1.0Microsoft Excel 2016 我复制了列出的示例 here (我的版本粘贴在下面):

######################################################################
#
# This program shows several examples of how to set up headers and
# footers with XlsxWriter.
#
# The control characters used in the header/footer strings are:
#
# Control Category Description
# ======= ======== ===========
# &L Justification Left
# &C Center
# &R Right
#
# &P Information Page number
# &N Total number of pages
# &D Date
# &T Time
# &F File name
# &A Worksheet name
#
# &fontsize Font Font size
# &"font,style" Font name and style
# &U Single underline
# &E Double underline
# &S Strikethrough
# &X Superscript
# &Y Subscript
#
# &[Picture] Images Image placeholder
# &G Same as &[Picture]
#
# && Miscellaneous Literal ampersand &
#
# See the main XlsxWriter documentation for more information.
#
# Copyright 2013-2018, John McNamara, jmcnamara@cpan.org
#
import xlsxwriter

workbook = xlsxwriter.Workbook('E:\\headers_footers.xlsx')
preview = 'Select Print Preview to see the header and footer'

######################################################################
#
# A simple example to start
#
worksheet1 = workbook.add_worksheet('Simple')
header1 = '&CHere is some centered text.'
footer1 = '&LHere is some left aligned text.'

worksheet1.set_header(header1)
worksheet1.set_footer(footer1)

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


######################################################################
#
# Insert a header image.
#
worksheet2 = workbook.add_worksheet('Image')
header2 = '&L&G'

# Adjust the page top margin to allow space for the header image.
worksheet2.set_margins(top=1.3)

worksheet2.set_header(header2, {'image_left': 'logo.png'})

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


######################################################################
#
# This is an example of some of the header/footer variables.
#
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])

######################################################################
#
# This example shows how to use more than one font
#
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 line wrapping
#
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()

在第 40 行,我更改了文件的目标位置,在第 68 行,我更改了图像的名称;除此之外,我没有改变任何东西。当我运行上述程序时,我看不到图像和字体。我在下面附上了截图:image

程序正确完成,没有抛出任何异常。大多数其他功能,如 set_columninsert_image 等都可以使用。但是 set_header 不是。

我不知道是否需要更多信息。如果需要,请随时提出相同的要求。

最佳答案

Excel 中的页眉和页脚不会显示在工作表的普通 View 中。正如示例的输出所示“选择打印预览以查看页眉和页脚”。

在打印预览模式下,输出如下所示:

enter image description here

或者在“页面布局” View 中看起来像这样:

enter image description here

关于python - xlsxwriter 的 set_header 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52402358/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com