2013-10-30 2 views
18

나는이 같은 테이블을 작성하고 싶습니다 :xlwt에 여러 열이있는 셀을 작성하는 방법은 무엇입니까?

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

어떻게 셀 Long Cell를 작성? 감사.

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

그러나이 같은 끝낼 :

내가 이런 식으로 작업을 수행하려고했습니다 지금까지 내가이 문서화되지 않은 말할 수

-------------------- 
| Long Cell |  | 
-------------------- 
| 1   | 2 | 
-------------------- 
+0

@ShivanRaptor 내 질문을 업데이트했습니다. 감사. –

답변

42

- 그럴 필요 그것을 찾으려면 소스 코드를 읽으십시오. 이 작업을 수행하는 Worksheet 클래스에는 두 가지 방법, write_mergemerge이 있습니다. merge은 기존 셀을 가져 와서 병합하는 반면 write_merge은 레이블을 작성하고 (write처럼) 동일한 항목 인 merge을 작성합니다.

두 셀 모두 병합 할 셀을 r1, r2, c1, c2으로 가져오고 선택적인 style 매개 변수를 허용합니다.

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

가 호출이 작동하는 방법에 대한 자세한 명시하기 : 또는

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') 

, merge 사용 :

sheet.write(top_row, left_column, 'Long Cell') 
sheet.merge(top_row, bottom_row, left_column, right_column) 
귀하의 예제에서

,이 간단한 호출 될 것이다

merge 소스를 가리키는 몇 가지 의견이 있습니다. ntial 문제 :

# 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. 

그러나이 같은 간단한 경우에 잘 될 것입니다.

+1

그게 좋습니다! 감사. –

관련 문제