惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

月光博客
月光博客
罗磊的独立博客
The GitHub Blog
The GitHub Blog
V
V2EX
Last Week in AI
Last Week in AI
博客园 - 聂微东
MyScale Blog
MyScale Blog
美团技术团队
L
LangChain Blog
博客园 - Franky
腾讯CDC
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园_首页
S
SegmentFault 最新的问题
爱范儿
爱范儿
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Stack Overflow Blog
Stack Overflow Blog
量子位
小众软件
小众软件
宝玉的分享
宝玉的分享
J
Java Code Geeks
Google DeepMind News
Google DeepMind News
D
Docker
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报

老董笔记

尚硅谷机构在哪?尚硅谷培训怎么样?靠谱吗-互联网IT百科 韩顺平介绍,传智讲师,开办泰牛,入尚硅谷等一系列-互联网IT百科 pandas多重索引标准样式(写入excel有空行)-互联网IT百科 cannot join with no overlapping index names-互联网IT百科 pandas多列变多行(即宽表变长表)melt和stack函数-互联网IT百科 pandas多行转多列(长表变宽表)pivot和unstack-互联网IT百科 Index contains duplicate entries, cannot reshape完美解决-互联网IT百科 single positional indexer is out-of-bounds-互联网IT百科 Can only compare identically-labeled Series objects-互联网IT百科 pandas transform用法详解(多个案例)-互联网IT百科 python四舍五入精确实现-互联网IT百科 pandas的groupby使用apply分组排序-互联网IT百科 index 0 is out of bounds for axis 0 with size 0-互联网IT百科 pandas分组过滤filter函数-互联网IT百科 联想Win10系统如何禁用触摸屏关闭触摸-互联网IT百科 groupby分组计算transform转换返回相同长度序列-互联网IT百科 brooks seo教程python教程,brooks seo教程网盘,布鲁seo资源-互联网IT百科 电脑右键文件夹一直转圈电卡死怎么回事-互联网IT百科 施琪嘉的心理成长课(荐)-互联网IT百科 百度SEO公司_SEO推广公司哪家好_SEO外包服务如何选-老董笔记 groupby后agg同1列用多个聚合函数、不同列用不同函数、自定义函数-互联网IT百科 pandas的groupby单列多列分组聚合运算-互联网IT百科 DataFrameGroupBy对象及分组个数、分组大小、组名索引、组数据详解-互联网IT百科 pandas中groupby之Grouper and axis must be same length-互联网IT百科 pandas中groupby的分组原理-互联网IT百科 pandas的groupby的使用详解大全-互联网IT百科 openpyxl单元格自动换行强制换行Alignment(wrapText=True)-互联网IT百科 python教程全套(可就业)-互联网IT百科 联想win10系统CPU显示100%,电脑呼呼响怎么回事-互联网IT百科 如何自制CPU,CPU原理是怎么样的?-互联网IT百科
row_ dimensions的RowDimension对象和column_dimensions的Col...
2021-04-08 · via 老董笔记

  上一篇文章设置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。
有宝贵意见可添加站长微信(底部),获取技术资料请到公众号(底部)。同行交流请加群 python学习会