当前位置: 首页 > 工具软件 > Dimensions > 使用案例 >

python row column_row_ dimensions的RowDimension对象和column_dimensions的ColumnDimension对象。...

王英彦
2023-12-01

上一篇文章设置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

-----------

c

==========

1

-----------

c

==========

1

-----------

c

==========

最后,我们再通过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_row)[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

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

==========

 类似资料: