2017-01-10 16 views
9

Như một ví dụ, tôi có một kịch bản chung mà kết quả đầu ra các kiểu bảng mặc định sử dụng python-docx (mã này chạy tốt):Làm thế nào để bạn giữ các hàng trong bảng với nhau trong python-docx?

import docx 
d=docx.Document() 
type_of_table=docx.enum.style.WD_STYLE_TYPE.TABLE 

list_table=[['header1','header2'],['cell1','cell2'],['cell3','cell4']] 
numcols=max(map(len,list_table)) 
numrows=len(list_table) 

styles=(s for s in d.styles if s.type==type_of_table) 


for stylenum,style in enumerate(styles,start=1): 
    label=d.add_paragraph('{}) {}'.format(stylenum,style.name)) 
    label.paragraph_format.keep_with_next=True 
    label.paragraph_format.space_before=docx.shared.Pt(18) 
    label.paragraph_format.space_after=docx.shared.Pt(0) 
    table=d.add_table(numrows,numcols) 
    table.style=style 
    for r,row in enumerate(list_table): 
     for c,cell in enumerate(row): 
      table.row_cells(r)[c].text=cell 


d.save('tablestyles.docx') 

Tiếp theo, tôi mở tài liệu, nhấn mạnh một bảng phân chia và dưới định dạng đoạn văn, chọn "Keep với tới", trong đó ngăn chặn thành công bàn từ được chia trên một trang:

enter image description here

đây là mã XML của bảng không bị hỏng:

enter image description here

Bạn có thể thấy dòng được đánh dấu hiển thị thuộc tính đoạn cần giữ bảng cùng nhau. Vì vậy, tôi đã viết chức năng này và bị mắc kẹt trong các mã trên d.save('tablestyles.docx') dòng:

def no_table_break(document): 
    tags=document.element.xpath('//w:p') 
    for tag in tags: 
     ppr=tag.get_or_add_pPr() 
     ppr.keepNext_val=True 


no_table_break(d)  

Khi tôi kiểm tra mã XML thẻ sở hữu đoạn được thiết lập đúng cách và khi tôi mở tài liệu Word, các "Keep với tới" hộp được chọn cho tất cả các bảng, nhưng bảng vẫn được chia trên các trang. Tôi có thiếu một thẻ XML hoặc một cái gì đó ngăn chặn điều này hoạt động đúng không?

+0

Tôi nghĩ bạn sẽ cần phải cụ thể hơn về hàng "mồ côi". Bước tiếp theo sẽ là xem bạn có thể hoàn thành kết quả sau khi sử dụng ứng dụng Word/UI không. Nếu bạn có thể thu hẹp nó theo cách đó, bạn có thể xác định phần tử/thuộc tính XML tạo nên sự khác biệt. 'w: cantSplit' có thể xác định xem một ô được chia trên các trang (với dòng của khóa học). – scanny

+0

@scanny tất cả những gì tôi có ý nghĩa bởi hàng mồ côi là một phần của một bảng nằm trên một trang và một phần khác của bảng là một trang khác. – LMc

+0

Câu hỏi đặt ra là liệu dấu ngắt nằm trên một hàng ngang hay bị phá vỡ trong hàng, như một phần của hàng trên một trang và phần còn lại của nó ở đầu trang tiếp theo. Đây là những hành vi riêng biệt (mis-). – scanny

Trả lời

1

Bạn có thể kiểm tra xem bảng có bị cắt hay không bằng ngắt trang và tạo bảng mới trước khi tạo bảng mới.

import docx 

MAX_PAGE_SIZE = 80 

d=docx.Document() 
type_of_table=docx.enum.style.WD_STYLE_TYPE.TABLE 

list_table=[['header1','header2'],['cell1','cell2'],['cell3','cell4']] 
numcols=max(map(len,list_table)) 
numrows=len(list_table) 

styles=(s for s in d.styles if s.type==type_of_table) 

current_page_row = 0 

for stylenum,style in enumerate(styles,start=1): 
    # calc the last row for this new style 
    next_last_row = current_page_row + numrows + 1 

    # add page break if needed and reset the page row 
    if next_last_row > MAX_PAGE_SIZE: 
     d.add_page_break() 
     current_page_row = 0 

    label=d.add_paragraph('{}) {}'.format(stylenum,style.name)) 
    # update the current page row counter 
    current_page_row += 1 

    label.paragraph_format.keep_with_next=True 
    label.paragraph_format.space_before=docx.shared.Pt(18) 
    label.paragraph_format.space_after=docx.shared.Pt(0) 

    table=d.add_table(numrows,numcols) 
    table.style=style 
    for r,row in enumerate(list_table): 
     for c,cell in enumerate(row): 
      table.row_cells(r)[c].text=cell 

    # update the current page row counter with the table size 
    current_page_row += numrows 

d.save('ctablestyles.docx') 
Các vấn đề liên quan