python - How can I edit column width with openpyxl
问题描述
I am trying to change the width of the first two columns in an excel sheet but I keep getting the following error:
wb.column_dimensions['A'].width = 25 AttributeError: 'Workbook' object has no attribute 'column_dimensions'
Here's my code:
from openpyxl import Workbook
wb = Workbook()
sheet = wb.active
wb.column_dimensions['A'].width = 25
wb.column_dimensions['B'].width = 25
sheet["A1"] = "Date"
sheet["B2"] = "Name"
解决方案
You need to address sheet
like this:
from openpyxl import Workbook
wb = Workbook()
sheet = wb.active
sheet.column_dimensions['A'].width = 25
^
sheet.column_dimensions['B'].width = 25
^
sheet["A1"] = "Date"
sheet["B2"] = "Name"
推荐阅读
- c++ - c ++将结构数组传递给函数
- react-native - 在 React Native 中保存数据
- python - 长轮询的 Ajax 无法在网页上显示 celery 结果
- java - 如何在 Janino 脚本中表示值列表
- php - 将JSON插入Mysql问题(ajax/php)
- docker - 使用docker将kafka主题数据写入redis
- react-native - 反应导航中 Stack 和 Swtich Navigator 之间的嵌套导航器之间的导航
- python - Virtualbox上的Python脚本非常慢
- javascript - 以编程方式附加脚本时未捕获的异常
- java - 如何通过共享对象正确使用信号?