如何使用python docx关闭Word文档中的单元格间距? (或任何其他包)

问题描述 投票:0回答:1

我有一个输入Word文档,其中一些表格的单元格间距设置为0.02“。我想使用下面使用python-docx包的代码关闭该单元格间距(或将其设置为0)。但是,当我在输入单词文件上运行代码并打开输出文件时,单元格间距仍然打开并设置为 0.02。

我相信下面的代码应该可以工作并将单元格间距设置为关闭或零。为什么 python-docx 所做的单元格间距更改不会反映在文件中?

from docx import Document
from docx.oxml import OxmlElement
from docx.oxml.ns import qn

def disable_cell_spacing_in_tables(file_path, output_path):
    """
    Disable cell spacing for all tables in the given Word document.
    
    :param file_path: Path to the input Word document.
    :param output_path: Path to save the modified Word document.
    """
    # Open the Word document
    doc = Document(file_path)
    
    # Iterate through all tables in the document
    for table in doc.tables:
        tbl = table._element
        tblPr = tbl.tblPr if tbl.tblPr is not None else OxmlElement('w:tblPr')
        if tbl.tblPr is None:
            tbl.append(tblPr)
        
        # Disable cell spacing
        tbl_cell_spacing = tblPr.find(qn('w:tblCellSpacing'))
        if tbl_cell_spacing is not None:
            tblPr.remove(tbl_cell_spacing)
        tbl_cell_spacing = OxmlElement('w:tblCellSpacing')
        tbl_cell_spacing.set(qn('w:w'), '0')
        tbl_cell_spacing.set(qn('w:type'), 'dxa')
        tblPr.append(tbl_cell_spacing)
    
    # Save the modified document
    doc.save(output_path)
    print(f"Modified document saved to {output_path}")

if __name__ == "__main__":
    input_file_path = 'input.docx'
    output_file_path = 'output.docx'
    disable_cell_spacing_in_tables(input_file_path, output_file_path)

这是输出文件中 Ms Word 菜单中的表格选项,仍设置为 0.02"。

Table Properties >> 选项 >> 允许单元格之间的间距仍处于打开状态

python openxml python-docx
1个回答
0
投票

Word 不仅在表格属性

tlbPr
中存储表格的单元格间距,而且还在表格行属性
trPr
中存储表格的单元格间距。因此,要删除它,需要从两个属性设置中删除它。

from docx import Document
from docx.oxml import OxmlElement
from docx.oxml.ns import qn

def disable_cell_spacing_in_tables(file_path, output_path):
    """
    Disable cell spacing for all tables in the given Word document.
    
    :param file_path: Path to the input Word document.
    :param output_path: Path to save the modified Word document.
    """
    # Open the Word document
    doc = Document(file_path)
    
    # Iterate through all tables in the document
    for table in doc.tables:
        tbl = table._element
        tblPr = tbl.tblPr if tbl.tblPr is not None else OxmlElement('w:tblPr')
        if tbl.tblPr is None:
            tbl.append(tblPr)
        
        # Disable cell spacing
        tbl_cell_spacing = tblPr.find(qn('w:tblCellSpacing'))
        if tbl_cell_spacing is not None:
            tblPr.remove(tbl_cell_spacing)
        #tbl_cell_spacing = OxmlElement('w:tblCellSpacing')
        #tbl_cell_spacing.set(qn('w:w'), '0')
        #tbl_cell_spacing.set(qn('w:type'), 'dxa')
        #tblPr.append(tbl_cell_spacing)
        
        for row in table.rows:
            tr = row._element
            trPr = tr.trPr if tr.trPr is not None else OxmlElement('w:trPr')
            tbl_cell_spacing = trPr.find(qn('w:tblCellSpacing'))
            if tbl_cell_spacing is not None:
                trPr.remove(tbl_cell_spacing)
            #tbl_cell_spacing = OxmlElement('w:tblCellSpacing')
            #tbl_cell_spacing.set(qn('w:w'), '0')
            #tbl_cell_spacing.set(qn('w:type'), 'dxa')
            #trPr.append(tbl_cell_spacing)
           
    # Save the modified document
    doc.save(output_path)
    print(f"Modified document saved to {output_path}")

顺便说一句:如果只是需要删除,则无需设置为0。

© www.soinside.com 2019 - 2024. All rights reserved.