【问题标题】:df.style : Setting specific whole row format bolddf.style :设置特定的整行格式粗体
【发布时间】:2019-11-07 09:46:17
【问题描述】:

我正在使用 imgkit 创建一个 pdf,其中包含从来自数据框的 html 创建的图像。我需要将整个特定行加粗 [rows: 0 and 7] 但还没有实现。我错过了什么?

html = df.style\
         .set_properties(**{'width': 200, 'background-color': '#eae2d5',
                            'color': 'black', 'font-size': '14px',
                            'text-align': 'left'},
                         subset=['var1'])\
         .set_properties(**{'color': '\033[1m'},  # BOLDING rows 1   
                         subset=['var1'][0])\   # Here is the error.    .
         .set_properties(**{'color': '\033[1m'},  # BOLDING rows 7   
                         subset=['var1'][7])\   # Here is the error.              
         .set_properties(**{'width': 80, 'background-color': '#eae2d5',
                            'color': 'black', 'font-size': '14px',
                            'text-align': 'center'},
                         subset=['var3', 'var4'])\
         .set_table_styles([{'selector': 'thead',
                             'props': [('background-color', '#b2361e'),
                                       ('color', 'white'),
                                       ('font-size', '18px')]}, ])\
         .highlight_null(null_color='white').hide_index().render()


到目前为止它说:“SyntaxError:行继续字符后的意外字符”

【问题讨论】:

    标签: python


    【解决方案1】:

    这更像是一个风格问题,所以请随意接受我的回答。

    首先,我建议查看 Python 中发生错误的特定行的错误消息。这将非常有帮助,因为我们可能看错了东西。例如,当我第一次尝试回读您的代码时,我的目光转向了这一行:

    'color': '\033[1m'

    我认为这不是一个有效的颜色,因为您的反斜杠在 0 上作为转义字符运行。此外,我不确定 [ 会做什么。

    其次,代码一般难以阅读,Python注重可读性。你愿意重构代码来帮助解决问题吗?也许是这样的?

    https://stackoverflow.com/a/4768979/11323304

    我希望这个答案对您有所帮助,欢迎来到 Stack!

    【讨论】:

    • 感谢您的回答。 StackOverflow 修改了我的原始代码,希望它现在保持原样。是的,基本上我在网上看到“\033[1m”是加粗的代码
    猜你喜欢
    • 1970-01-01
    • 2021-01-14
    • 2018-07-29
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 2015-09-23
    • 2012-07-23
    • 1970-01-01
    相关资源
    最近更新 更多