有没有办法用pandas.ExcelWriter自动调整Excel列宽? [英] Is there a way to auto-adjust Excel column widths with pandas.ExcelWriter?

查看:13606
本文介绍了有没有办法用pandas.ExcelWriter自动调整Excel列宽?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被要求生成一些Excel报告。我目前在使用大熊猫的数据,所以我自然要使用pandas.ExcelWriter方法生成这些报告。但固定的列宽是一个问题。



我迄今为止的代码很简单。说我有一个名为'df'的数据框:

  writer = pd.ExcelWriter(excel_file_path)
df.to_excel作者,sheet_name =摘要)

我在看大熊猫代码,我不真的看到任何设置列宽的选项。在宇宙中有没有一个技巧,使得列可以自动调整数据?或者有什么我可以做的事情后,xlsx文件来调整列宽度?



(我正在使用OpenPyXL库,并生成.xlsx文件 - 如果有任何区别)。



谢谢。

解决方案

现在可能没有自动的方法,但是当你使用openpyxl, (根据用户 Bufke 的另一个答案,在手动操作)允许您指定一个合理的值(字符宽度):

  writer.sheets ['Summary']。column_dimensions ['A']。width = 15 


I am being asked to generate some Excel reports. I am currently using pandas quite heavily for my data, so naturally I would like to use the pandas.ExcelWriter method to generate these reports. However the fixed column widths are a problem.

The code I have so far is simple enough. Say I have a dataframe called 'df':

writer = pd.ExcelWriter(excel_file_path)
df.to_excel(writer, sheet_name="Summary")

I was looking over the pandas code, and I don't really see any options to set column widths. Is there a trick out there in the universe to make it such that the columns auto-adjust to the data? Or is there something I can do after the fact to the xlsx file to adjust the column widths?

(I am using the OpenPyXL library, and generating .xlsx files - if that makes any difference.)

Thank you.

解决方案

There is probably no automatic way to do it right now, but as you use openpyxl, the following line (adapted from another answer by user Bufke on how to do in manually) allows you to specify a sane value (in character widths):

writer.sheets['Summary'].column_dimensions['A'].width = 15

这篇关于有没有办法用pandas.ExcelWriter自动调整Excel列宽?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆