Python Module_openpyxl_styles 样式处理
目录
前言
继续搞Python对Excel文件的处理,这次主要解决如何使用Openpyxl模块的Styles来生成一个具有个性化样式的Excel文件。本篇基本算是翻译了部分实用的官方文档内容,推荐小伙伴们最好的学习方法,莫过于阅读官方文档。: )
官当文档传送门:http://openpyxl.readthedocs.org/en/default/styles.html#worksheet-additional-properties
系统软件
- 系统
- Windows 8.1
- 软件
- Python 3.4.3
- IPython 4.0.0
Working with styles
使用Excel样式函数
Introduction(简介)
Styles are used to change the look of your data while displayed on screen. They are also used to determine the number format being used for a given cell or range of cells.
Styles是用于改变你的希望显示的数据的样式。也可以用于设置指定的单元格或单元区域的数字格式。
Styles can be applied to the following aspects
- font to set font size, color, underlining, etc.(能够设定字体的大小、颜色、下划线等属性)
- fill to set a pattern or color gradient(能够设置单元格的填充样式或颜色渐变)
- border to set borders on a cell(能够设置单元格的边框)
- cell alignment(能够设置单元格的对齐)
- protection(能够设置访问限制)
Styles模块
The following are the default values(下面是函数的参数缺省值)
>>> from openpyxl.styles import PatternFill,Border,Side,Alignment,Protection,Font
>>> font = Font(name='Calibri',
... size=11,
... bold=False,
... italic=False,
... vertAlign=None,
... underline='none',
... strike=False,
... color='FF000000')
>>> fill = PatternFill(fill_type=None,
... start_color='FFFFFFFF',
... end_color='FF000000')
>>> border = Border(left=Side(border_style=None,
... color='FF000000'),
... right=Side(border_style=None,
... color='FF000000'),
... top=Side(border_style=None,
... color='FF000000'),
... bottom=Side(border_style=None,
... color='FF000000'),
... diagonal=Side(border_style=None,
... color='FF000000'),
... diagonal_direction=0,
... outline=Side(border_style=None,
... color='FF000000'),
... vertical=Side(border_style=None,
... color='FF000000'),
... horizontal=Side(border_style=None,
... color='FF000000')
... )
>>> alignment=Alignment(horizontal='general',
... vertical='bottom',
... text_rotation=0,
... wrap_text=False,
... shrink_to_fit=False,
... indent=0)
>>> number_format = 'General'
>>> protection = Protection(locked=True,
... hidden=False)
>>>
注意:
Styles are shared between objects and once they have been assigned they cannot be changed. This stops unwanted side-effects such as changing the style for lots of cells when instead of only one.
不同的对象之间是可以共享同一个Styles的,并且一旦为对象指定了Styles之后就不可以再次更改。这是为了在更改很多的单元格的Styles而不仅只是更改一个单元格时能够避免不必要的副作用。
>>> from openpyxl.styles import colors
>>> from openpyxl.styles import Font, Color
>>> from openpyxl.styles import colors
>>> from openpyxl import Workbook
>>> wb = Workbook()
>>> ws = wb.active
>>>
>>> a1 = ws['A1']
>>> d4 = ws['D4']
>>> ft = Font(color=colors.RED) #定义一个可以共享的Styles
>>> a1.font = ft
>>> d4.font = ft
>>>
>>> a1.font.italic = True # is not allowed 对象在指定Styles后是不允许被更改的
>>>
>>> # If you want to change the color of a Font, you need to reassign it::
>>> # 如果你想更改个别对象的Styles需要重新定义,且只会影响到个别对象
>>> a1.font = Font(color=colors.RED, italic=True) # the change only affects A1
Copying styles
Styles can also be copied(Styles可以被复制)
>>> from openpyxl.styles import Font
>>>
>>> ft1 = Font(name='Arial', size=14)
>>> ft2 = ft1.copy(name="Tahoma") #复制并修改特定属性
>>> ft1.name
'Arial'
>>> ft2.name
'Tahoma'
>>> ft2.size # copied from the 14.0
Basic Font Colors
Colors are usually RGB or aRGB hexvalues. The colors module contains some constants
Colors通常是RGB或者是RGB的十六进制表示。Colors模块包含了一些常量
>>> from openpyxl.styles import Font
>>> from openpyxl.styles.colors import RED
>>> font = Font(color=RED) #RGB
>>> font = Font(color="FFBB00") #RGB hexvalues
There is also support for legacy indexed colors as well as themes and tints
Colors也支持索引颜色、主题和色彩
>>> from openpyxl.styles.colors import Color
>>> c = Color(indexed=32) #legacy indexed colors 定制好的Color通过索引调用
>>> c = Color(theme=6, tint=0.5)
Applying Styles
Styles are applied directly to cells
Styles直接应用于单元格
>>> from openpyxl.workbook import Workbook
>>> from openpyxl.styles import Font, Fill
>>> wb = Workbook()
>>> ws = wb.active
>>> c = ws['A1'] #获取单元格对象
>>> c.font = Font(size=12) #直接修改单元格对象的字体样式
Styles can also applied to columns and rows but note that this applies only to cells created (in Excel) after the file is closed. If you want to apply styles to entire rows and columns then you must apply the style to each cell yourself. This is a restriction of the file format
Styles也可以应用于列和行,但是需要注意的是这种应用只能适用于在关闭文件之后创建的单元格。如果你想应用Style于全部的行和列,你必须为每一个单元格都应用Style。这是由于文件格式的制约
>>> col = ws.column_dimensions['A'] #获取A列的样式
>>> col.font = Font(bold=True)
>>> row = ws.row_dimensions[1] #获取1行的样式
>>> row.font = Font(underline="single")
Edit Page Setup
>>> from openpyxl.workbook import Workbook
>>>
>>> wb = Workbook()
>>> ws = wb.active
>>> #设置页面的样式
>>> ws.page_setup.orientation = ws.ORIENTATION_LANDSCAPE
>>> ws.page_setup.paperSize = ws.PAPERSIZE_TABLOID
>>> ws.page_setup.fitToHeight = 0
>>> ws.page_setup.fitToWidth = 1
Edit Print Options
>>> from openpyxl.workbook import Workbook
>>>
>>> wb = Workbook()
>>> ws = wb.active
>>>
>>> ws.print_options.horizontalCentered = True #水平居中
>>> ws.print_options.verticalCentered = True #垂直居中
Header / Footer
Headers and footers use their own formatting language. This is fully supported when writing them.but, due to the complexity and the possibility of nesting, only partially when reading them.
头部和尾部使用它们自身的格式化语言。当你写的时候是完全支持的。但是由于复制性和嵌套的可能性,让你读取的时候可能只能读取到一个部分。
>>> from openpyxl.workbook import Workbook
>>>
>>> wb = Workbook()
>>> ws = wb.worksheets[0]
>>> #设置文件头部和页尾的样式
>>> ws.header_footer.center_header.text = 'My Excel Page'
>>> ws.header_footer.center_header.font_size = 14
>>> ws.header_footer.center_header.font_name = "Tahoma,Bold"
>>> ws.header_footer.center_header.font_color = "CC3366"
Python Module_openpyxl_styles 样式处理的更多相关文章
- PEP 8 - Python代码样式指南
PEP 8 - Python代码样式指南 PEP: 8 标题: Python代码风格指南 作者: Guido van Rossum <python.org上的guido>,Barry Wa ...
- pycharm python模版样式
问题: 我想在创建新的一些python程序的时候,希望在新文件开头添加python版本声明和一些关于时间相关的模版数据 那,如何解决? 1. pycharm ---> setting ---&g ...
- python 将pdf分页后插入至word中
所用技术 1. python编程基础 2. 使用pyPdf 3. 使用python操作word 4. 正则表达式的使用 5. windows的bat编程 下面是一个pyPdf库使用的示例: from ...
- Python中dunder名称的来历
版权声明:博客为作者原创,允许转载,但必须注明原文地址:https://www.cnblogs.com/byronxie/p/10741084.html 在 Python 中,我们经常会看到被双下划线 ...
- Python人工智能学习笔记
Python教程 Python 教程 Python 简介 Python 环境搭建 Python 中文编码 Python 基础语法 Python 变量类型 Python 运算符 Python 条件语句 ...
- python爬虫积累(一)--------selenium+python+PhantomJS的使用(转)
阅读目录 一.Selenium介绍 二.爬虫为什么要用selenium? 三.PhantomJS介绍 四.PhantomJS安装 五.操作实战 六.在此推荐虫师博客的学习资料 selenium + p ...
- python爬虫积累(一)--------selenium+python+PhantomJS的使用
最近按公司要求,爬取相关网站时,发现没有找到js包的地址,我就采用selenium来爬取信息,相关实战链接:python爬虫实战(一)--------中国作物种质信息网 一.Selenium介绍 Se ...
- python操作word
python教程(百度经验) Python 操作Word(Excel.PPT等通用) import win32comfrom win32com.client import Dispatch, co ...
- python代码规范整理
规范参考源: 1.pep8(python代码样式规范):中文文档 https://blog.csdn.net/ratsniper/article/details/78954852 2.pep ...
随机推荐
- Windows Server IIS设置计划任务重启
Windows Server ,IIS设置计划任务重启. echo ----->>c:\log_iisreset.txt && echo %date% %time% &g ...
- 新手的Linux zcat命令示例
Zcat是一个命令行实用程序,用于查看压缩文件的内容.它将压缩文件扩展为标准输出,允许您查看内容. 分类:Linux命令操作系统 2018-08-13 00:00:00 通常,使用gzip压缩的文件可 ...
- Java并发(基础知识)—— 创建、运行以及停止一个线程
在计算机世界,当人们谈到并发时,它的意思是一系列的任务在计算机中同时执行.如果计算机有多个处理器或者多核处理器,那么这个同时性是真实发生的:如果计算机只有一个核心处理器那么就只是表面现象. 现代所有的 ...
- java面试(反射)05
1.什么是反射 JAVA反射机制是在运行状态中,对于任意一个类,都能够获取这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取类信息以及动态调用对象内容就称为jav ...
- ThinkPHP中的SQL结果分析
ThinkPHP中的SQL结果分析 在进行结果判断时需要注意. 一.查询SQL: 可能出现的情况:1.查询成功,返回正常数据.2.查询成功,但是没有数据,返回NULL.3.查询错误,写错关键字.结果: ...
- json格式字符串转字典
//json格式字符串转字典+ (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString { if (jsonStr ...
- 2018 ACM-ICPC 区域赛(青岛站)题解整理
题目链接 C - Flippy Sequence(组合数学+分类讨论) 两区间异或一下,分段考虑,如果全为0则任选两相同区间,答案为$C_{n+1}^{2}=\frac{n(n+1)}{2}$,只有一 ...
- java初学者的Springmvc04笔记
Springmvc04 Springmvc的全局异常处理 springmvc与spring的整合 myBatis 1.Springmvc的全局异常处理 作用:一次配置,对于controller层的所有 ...
- Redis(一)
1 单机MySQL的美好时代2 Memcached(缓存)+MySQL+垂直拆分3 MySQL主从读写分离4 分库分表+水平拆分+mysql拆分5 MySQL的扩展瓶颈6 为什么使用NoSQLNoSQ ...
- sqlserver表值函数调用方式
Connection conn = sqlServerManage.sqlServerConn(); Statement stmt; ResultSet rs; // 组装sql StringBuff ...