我使用StyleFrame从熊猫导出到Excel。
默认情况下,单元格被格式化为“包装文本”和“收缩以适合”。(如何)我可以更改这些设置吗?
API文档描述了utils模块包含用于样式元素的最广泛使用的值,只要Excel识别它,就可以直接使用不存在于utils模块中的值。
在这种情况下,我需要为Excel指定什么?如何/在哪里可以找到Excel所期望的内容?事先非常感谢!
我尝试过的例子:
这个代码工作得很好:
sf.apply_column_style(cols_to_style=['A'], styler_obj=Styler(bg_color=utils.colors.blue))但我的问题是,我不知道该更改什么来关闭文本包装并缩小以适应选项:
sf.apply_column_style(cols_to_style=['A'], styler_obj=Styler(text_control=wrap_text.none))
NameError: name 'wrap_text' is not defined
sf.apply_column_style(cols_to_style=['A'], styler_obj=Styler(text_control=utils.wrap_text.none))
AttributeError: module 'StyleFrame.utils' has no attribute 'wrap_text'
sf.apply_column_style(cols_to_style=['A'], styler_obj=Styler(utils.wrap_text.none))
AttributeError: module 'StyleFrame.utils' has no attribute 'wrap_text'
sf.apply_column_style(cols_to_style=['A'], styler_obj=Styler(wrap_text=False))
TypeError: __init__() got an unexpected keyword argument 'wrap_text'发布于 2017-12-23 14:19:27
在版本1.3中,可以直接将wrap_text和shrink_to_fit传递给Styler,例如no_wrap_text_style = Styler(wrap_text=False)
发布于 2017-12-11 21:57:30
坏消息是,在当前版本中,您不能很好地做到这一点。
好消息是,您现在可以对其进行猴子修补,我将在下一个版本中公开它的API。
from openpyxl.styles import (PatternFill, Style, Color, Border,
Side, Font, Alignment, Protection)
from StyleFrame import StyleFrame, Styler, utils
def my_create_style(self):
side = Side(border_style=self.border_type, color=utils.colors.black)
border = Border(left=side, right=side, top=side, bottom=side)
return Style(font=Font(name=self.font, size=self.font_size, color=Color(self.font_color),
bold=self.bold, underline=self.underline),
fill=PatternFill(patternType='solid', fgColor=self.bg_color),
alignment=Alignment(horizontal='center', vertical='center',
wrap_text=False, # use True/False as needed
shrink_to_fit=False, # use True/False as needed
indent=0),
border=border,
number_format=self.number_format,
protection=Protection(locked=self.protection))
Styler.create_style = my_create_style
# rest of code请记住,这将改变所有Styler对象的行为。如果您想要更精细的控制,您可以对单个Styler实例进行猴子修补,但它需要更多的创造性:
from functools import partial
# ... and rest of imports from above example
# same code as above
a_style = Styler(bg_color=utils.colors.blue)
# this needs to be done BEFORE applying
a_style.create_style = partial(my_create_style, a_style)
sf.apply_column_style(cols_to_style=['A'], styler_obj=a_style)https://stackoverflow.com/questions/47761063
复制相似问题