【问题标题】:When converting Dataframe to HTML is it possible to set column maximum width将 Dataframe 转换为 HTML 时,是否可以设置列最大宽度
【发布时间】:2021-12-19 19:37:10
【问题描述】:

我的 HTML 不太好.. 所以也许可以通过使用类来轻松解决这个问题...但是在网上没有找到任何示例...

我想将表格列设置为最大宽度。如果文本单元格大于单元格,则执行分词。

我找到了我需要的示例:

<table border="1" class="dataframe table table-bordered table-striped table-hover fixed">
    <col width="20px" />
    <col width="30px" />
    <col width="140px" />
    <col width="40px" />
    <col width="40px" />
  <thead>
    <tr>
      <th>Group</th>
      <th>Start Address (decimal)</th>
      <th>Length (decimal)</th>
      <th>Register</th>
      <th>Bits</th>
    </tr>
  </thead>
</table>

等等,正是我想要的..

但我不知道如何使用 panda.to_html 函数获得相同的行为...

有什么提示吗?

谢谢

【问题讨论】:

    标签: pandas styles export-to-html


    【解决方案1】:

    查看pandas documentation for to_html

    有一个选项可以定义列表中的列间距,每列的宽度作为列表中的一个元素:

    df.to_html(col_space=[20,30,140,40,40])
    

    编辑: 对此进行更深入的研究,我不确定如何设置您正在寻找的 最大 列宽,但通过替换生成的 html 中的文本来插入 col width 提出了一个黑客解决方法:

    .replace('<thead>','<col width="20px" />\n<col width="30px" />\n<col width="140px" />\n<col width="40px" />\n<col width="40px" />\n<thead>')
    

    完整示例:

    df = pd.DataFrame({'Group':['Short Text','Longer Text','Really Really Long Text'],
                    'Start Address (decimal)':['Short Text','Longer Text','Really Really Long Text'],
                    'Length (decimal)':['Short Text','Longer Text','Really Really Long Text'],
                    'Register':['Short Text','Longer Text','Really Really Long Text'],
                    'Bits':['Short Text','Longer Text','Really Really Long Text']})
    
    html = df.to_html(classes=['table-bordered','table-striped','table-hover fixed'],
                    index=False) \
                .replace('<thead>','<col width="20px" />\n<col width="30px" />\n<col width="140px" />\n<col width="40px" />\n<col width="40px" />\n<thead>')
    

    输出

    <table border="1" class="dataframe table-bordered table-striped table-hover fixed">
      <col width="20px" />
    <col width="30px" />
    <col width="140px" />
    <col width="40px" />
    <col width="40px" />
    <thead>
        <tr style="text-align: right;">
          <th>Group</th>
          <th>Start Address (decimal)</th>
          <th>Length (decimal)</th>
          <th>Register</th>
          <th>Bits</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>Short Text</td>
          <td>Short Text</td>
          <td>Short Text</td>
          <td>Short Text</td>
          <td>Short Text</td>
        </tr>
        <tr>
          <td>Longer Text</td>
          <td>Longer Text</td>
          <td>Longer Text</td>
          <td>Longer Text</td>
          <td>Longer Text</td>
        </tr>
        <tr>
          <td>Really Really Long Text</td>
          <td>Really Really Long Text</td>
          <td>Really Really Long Text</td>
          <td>Really Really Long Text</td>
          <td>Really Really Long Text</td>
        </tr>
      </tbody>
    </table>
    

    虽然不理想,但它应该生成接近您在问题中发布的最终结果的 html。

    【讨论】:

    • 是的,但这并没有指定最大宽度,而是最小宽度。
    猜你喜欢
    • 2020-02-24
    • 2016-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-19
    • 2020-11-26
    • 2017-01-21
    相关资源
    最近更新 更多