您的位置: 网站首页> openpyxl教程> 当前文章
row_ dimensions的RowDimension对象和column_dimensions的ColumnDimension对象。
老董-我爱我家房产SEO2021-04-08172围观,115赞
上一篇文章设置sheet行高和列宽的时候用过row_dimensions 和 column_dimensions,他们是sheet对象的两个属性。此外,row_ dimensions包含RowDimension对象,column_dimensions 包含ColumnDimension对象。我们本节通过dir函数研究下他们。
首先,你必须设置行高和列宽,否则下面的代码没有任何输出。
# -*- coding: utf-8 -*- from openpyxl import Workbook wb = Workbook() # 默认生成一个名为Sheet的sheet # 创建sheet for name in ['a','b']: ws = wb.create_sheet(name) lis = [1,2,3,4,5,6] # 追加行 for sheet in wb: for i in range(5): sheet.append(lis) for sheet in wb: res_row = sheet.row_dimensions.items() for i,obj in res_row: print(i,obj) res_col = sheet.column_dimensions.items() for i,obj in res_col: print(i,obj) wb.save('test.xlsx')
设置了行高和列宽后,我们可以输出RowDimension和ColumnDimension对象。
# -*- coding: utf-8 -*- from openpyxl import Workbook wb = Workbook() # 默认生成一个名为Sheet的sheet # 创建sheet for name in ['a','b']: ws = wb.create_sheet(name) lis = [1,2,3,4,5,6] # 追加行 for sheet in wb: for i in range(5): sheet.append(lis) for sheet in wb: sheet.row_dimensions[1].width = 20 for sheet in wb: sheet.column_dimensions['c'].width = 20 for sheet in wb: res_row = sheet.row_dimensions.items() for i,obj in res_row: print(i,obj) print('-----------') res_col = sheet.column_dimensions.items() for i,obj in res_col: print(i,obj) print('==========') wb.save('test.xlsx')
1 <openpyxl.worksheet.dimensions.RowDimension object at 0x000002A850172B40> ----------- c <openpyxl.worksheet.dimensions.ColumnDimension object at 0x000002A850172CF0> ========== 1 <openpyxl.worksheet.dimensions.RowDimension object at 0x000002A850172BD0> ----------- c <openpyxl.worksheet.dimensions.ColumnDimension object at 0x000002A850172D38> ========== 1 <openpyxl.worksheet.dimensions.RowDimension object at 0x000002A850172C60> ----------- c <openpyxl.worksheet.dimensions.ColumnDimension object at 0x000002A850172DC8> ==========
最后,我们再通过dir函数看看RowDimension和ColumnDimension的属性和方法,也就可以看到可以对excel做的操作。
# -*- coding: utf-8 -*- from openpyxl import Workbook wb = Workbook() # 默认生成一个名为Sheet的sheet # 创建sheet for name in ['a','b']: ws = wb.create_sheet(name) lis = [1,2,3,4,5,6] # 追加行 for sheet in wb: for i in range(5): sheet.append(lis) for sheet in wb: sheet.row_dimensions[1].width = 20 for sheet in wb: sheet.column_dimensions['c'].width = 20 sheet = wb['a'] res_row = sheet.row_dimensions.items() i,obj = list(res_row)[0] # 转为列表 dict_items对象不支持索引 for content in dir(obj): print(content) print('-----------') res_col = sheet.column_dimensions.items() i,obj = list(res_col )[0] for content in dir(obj): print(content) print('==========') wb.save('test.xlsx')
__class__ __copy__ __delattr__ __dict__ __dir__ __doc__ __eq__ __fields__ __format__ __ge__ __getattribute__ __gt__ __hash__ __init__ __init_subclass__ __iter__ __le__ __lt__ __module__ __ne__ __new__ __reduce__ __reduce_ex__ __repr__ __setattr__ __sizeof__ __slots__ __str__ __subclasshook__ __weakref__ _style alignment border collapsed customFormat customHeight fill font has_style height hidden ht index number_format outlineLevel outline_level parent pivotButton protection quotePrefix r s style style_id thickBot thickTop width ----------- __class__ __copy__ __delattr__ __dict__ __dir__ __doc__ __eq__ __fields__ __format__ __ge__ __getattribute__ __gt__ __hash__ __init__ __init_subclass__ __iter__ __le__ __lt__ __module__ __ne__ __new__ __reduce__ __reduce_ex__ __repr__ __setattr__ __sizeof__ __slots__ __str__ __subclasshook__ __weakref__ _style alignment auto_size bestFit border collapsed customWidth fill font has_style hidden index max min number_format outlineLevel outline_level parent pivotButton protection quotePrefix reindex style style_id to_tree width ==========
很赞哦!
python编程网提示:转载请注明来源www.python66.com。
有宝贵意见可添加站长微信(底部),获取技术资料请到公众号(底部)。同行交流请加群
相关文章
文章评论
-
row_ dimensions的RowDimension对象和column_dimensions的ColumnDimension对象。文章写得不错,值得赞赏
站点信息
- 网站程序:Laravel
- 客服微信:a772483200