Python 如何在xlwt中编写具有多列的单元格?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19672760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:20:12  来源:igfitidea点击:

How to write a cell with multiple columns in xlwt?

pythonexcelxlwt

提问by Bin Wang

I'd like to write a table like this:

我想写一个这样的表:

----------------
| Long Cell    |
----------------
| 1    | 2     |
----------------

How to write the cell Long Cell? Thanks.

单元格怎么写Long Cell?谢谢。

I've tried to do it like this:

我试过这样做:

sheet.write(0, 0, 'Long Cell')
sheet.write(1, 0, 1)
sheet.write(1, 1, 2)

But it end up like:

但它最终像:

--------------------
| Long Cell |      |
--------------------
| 1         | 2    |
--------------------

采纳答案by Peter DeGlopper

As far as I can tell, this isn't documented - you have to read the source code to find it. There are two methods on the Worksheetclass to do this, write_mergeand merge. mergetakes existing cells and merges them, while write_mergewrites a label (just like write) and then does the same stuff mergedoes.

据我所知,这没有记录 - 您必须阅读源代码才能找到它。类中有两种方法Worksheet可以执行此操作,write_merge并且merge. merge获取现有单元格并合并它们,同时write_merge写入一个标签(就像write),然后执行相同的merge操作。

Both take the cells to merge as r1, r2, c1, c2, and accept an optional styleparameter.

两者都将单元格合并为r1, r2, c1, c2,并接受一个可选style参数。

From your example, this would be the simplest call:

从您的示例中,这将是最简单的调用:

sheet.write_merge(0, 0, 0, 1, 'Long Cell')
sheet.write(1, 0, 1)
sheet.write(1, 1, 2)

To be more explicit about how the call works:

更明确地了解调用的工作方式:

top_row = 0
bottom_row = 0
left_column = 0
right_column = 1
sheet.write_merge(top_row, bottom_row, left_column, right_column, 'Long Cell')

Alternatively, using merge:

或者,使用merge

sheet.write(top_row, left_column, 'Long Cell')
sheet.merge(top_row, bottom_row, left_column, right_column)

mergehas some comments in the source pointing out potential problems:

merge源中有一些评论指出了潜在的问题:

    # Problems: (1) style to be used should be existing style of
    # the top-left cell, not an arg.
    # (2) should ensure that any previous data value in
    # non-top-left cells is nobbled.
    # Note: if a cell is set by a data record then later
    # is referenced by a [MUL]BLANK record, Excel will blank
    # out the cell on the screen, but OOo & Gnu will not
    # blank it out. Need to do something better than writing
    # multiple records. In the meantime, avoid this method and use
    # write_merge() instead.
    # Problems: (1) style to be used should be existing style of
    # the top-left cell, not an arg.
    # (2) should ensure that any previous data value in
    # non-top-left cells is nobbled.
    # Note: if a cell is set by a data record then later
    # is referenced by a [MUL]BLANK record, Excel will blank
    # out the cell on the screen, but OOo & Gnu will not
    # blank it out. Need to do something better than writing
    # multiple records. In the meantime, avoid this method and use
    # write_merge() instead.

But it would be fine for a simple case like this.

但是对于像这样的简单案例就可以了。