使用openpyxl将打印标题添加到工作表

我无法使用openpyxl将打印标题添加到我的Excel工作表中。 已命名的范围已正确创build,但它不能正确地成为打印标题的命名范围。 问题似乎是名为范围的打印标题的范围是不正确的(至less,如果我正确理解)。 如果我在Excel中手动创build打印标题,而不是作为工作表名称的作用域,范围始终为“工作簿”。 以下是我正在使用的代码的相关代码片段:

wb = Workbook() ws = wb.active ws.add_print_title(1, rows_or_cols='rows') 

据我可以告诉add_print_title只需要添加到命名范围的行数或列数,以及是否添加行和列。 就像我说的,这会正确地创build一个命名的范围,它不会将该命名范围放入打印标题中。 也许我错过了一些愚蠢的东西,但是我已经用尽了所有的Googlesearch和我的一点知识。

根据testing案例你正确地调用它:

 def test_print_titles(): wb = Workbook() ws1 = wb.create_sheet() ws2 = wb.create_sheet() ws1.print_title_rows = '1:2' # NOTE this was depricated, old code was ws1.add_print_title(2) ws2.print_title_cols = '1:3' # NOTE: this replaces depricated add_print_title, code was ws2.add_print_title(3, rows_or_cols='cols') def mystr(nr): return ','.join(['%s!%s' % (sheet.title, name) for sheet, name in nr.destinations]) actual_named_ranges = set([(nr.name, nr.scope, mystr(nr)) for nr in wb.get_named_ranges()]) expected_named_ranges = set([('_xlnm.Print_Titles', ws1, 'Sheet1!$1:$2'), ('_xlnm.Print_Titles', ws2, 'Sheet2!$A:$C')]) assert(actual_named_ranges == expected_named_ranges) 

看来这是一个已知的问题,但现在有一个修复,所以下一个版本应该照顾它。 你可以通过自己编辑文件来修复它,如下面的diff所示:

https://bitbucket.org/openpyxl/openpyxl/pull-request/47/fix-issue-417-creating-print-titles/diff

我不确定我们(能)如何支持这个,因为它似乎使用了规范中没有涉及的东西。 我从来没有使用过这种方法,所以我不熟悉它。

请提交两个文件的错误:openpyxl产生了什么; 它应该是什么。