【问题标题】:Python-docx, how to set cell width in tables?Python-docx,如何在表格中设置单元格宽度?
【发布时间】:2017-08-20 11:01:04
【问题描述】:

如何在表格中设置单元格宽度?,到目前为止我得到了:

from docx import Document
from docx.shared import Cm, Inches

document = Document()
table = document.add_table(rows=2, cols=2)
table.style = 'TableGrid' #single lines in all cells
table.autofit = False

col = table.columns[0] 
col.width=Inches(0.5)
#col.width=Cm(1.0)
#col.width=360000 #=1cm

document.save('test.docx')

无论我在col.width中设置什么数字或单位,它的宽度都不会改变。

【问题讨论】:

    标签: python ms-word python-docx column-width


    【解决方案1】:

    试试这个。这段代码对我有用。

    table2.cell(0,0).width = Inches(1.0)                                                
    table2.cell(0,1).width = Inches(1.0)
    

    【讨论】:

      【解决方案2】:

      您可能还想使用 WD_ROW_HEIGHT_RULE,它可以绕过表格中的自动调整。

      for row in table.rows:  
          row.height_rule = WD_ROW_HEIGHT_RULE.EXACTLY
      

      如果需要,这里是doc。

      【讨论】:

        【解决方案3】:

        table = documento.add_table(rows=1, cols=3, style="表格网格") table.alignment = WD_ALIGN_PARAGRAPH.CENTER

                table.allow_autofit = True
                table.columns[0].width = Cm(3.5)
                table.columns[1].width = Cm(7.5)
                table.columns[2].width = Cm(5.5)
        

        【讨论】:

          【解决方案4】:

          简答:单独设置单元格宽度。

          for cell in table.columns[0].cells:
              cell.width = Inches(0.5)
          

          python-docx 在您设置列宽时执行您告诉它执行的操作。问题是 Word 忽略了它。其他客户端,如 LibreOffice,尊重列宽设置。

          .docx 文件是 XML 格式的(因此文件扩展名中有“x”后缀)。表格的 XML 词汇表有一个列宽的位置和一个单元格宽度的位置。说到这个细节,谁会注意什么是有点烦恼的。一个共同点是每个人都尊重在单个单元格级别设置的明确宽度。这对我来说没有多大意义,但这就是让它发挥作用所需要的。在你的程序中有一个处理细节的函数可能是有意义的:

          def set_col_widths(table):
              widths = (Inches(1), Inches(2), Inches(1.5))
              for row in table.rows:
                  for idx, width in enumerate(widths):
                      row.cells[idx].width = width
          

          如果您的表格合并了单元格,这会变得有点复杂,这实际上可能是 Word 忽略列宽的原因;在某些合并单元格的情况下,它们是模棱两可的。

          【讨论】:

          • 可以跳过带有for cell, width in zip(row.cells, (Inches(1), Inches(2), Inches(1.5))): cell.width = width的索引
          • 对于 LibreOffice,我还必须更改列宽 (for idx, col in enumerate(table.columns): col.width = widths[idx])
          • table_columns[0].cells 中的单元格:cell.width = Inches(0.5) 不工作,不设置宽度!
          • 第一个代码sn-p有错别字。应该是:for cell in table.columns[0].cells
          • 我修正了错字。
          【解决方案5】:

          对于 LibreOffice,我必须设置:

          table.autofit = False 
          table.allow_autofit = False
          

          接下来,设置给定的列和单元格宽度

          table.columns[0].width = Inches(1.0)
          table.rows[0].cells[0].width = Inches(1.0)
          

          【讨论】:

            【解决方案6】:

            docs of python_docx

            allow_autofit 属性默认设置为 True,这意味着设置的宽度不会生效,所以: table.allow_autofit = False

            【讨论】:

              猜你喜欢
              • 2021-06-02
              • 1970-01-01
              • 2014-02-10
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2019-01-01
              • 1970-01-01
              相关资源
              最近更新 更多