首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Python的xlrd和xlutils保存Excel格式的问题

用Python的xlrd和xlutils保存Excel格式的问题
EN

Stack Overflow用户
提问于 2015-01-30 16:24:54
回答 1查看 5.1K关注 0票数 2

简单地说,我想将一个Excel文件的所有格式保存到另一个Excel文件中。但是,尽管使用了formatting_info=True标志,格式只显示在已更改行中所有未更改的单元格中。有什么建议吗?

代码语言:javascript
复制
import xlrd, xlutils
from xlrd import open_workbook
from xlutils.copy import copy

inBook = xlrd.open_workbook(r"path/to/file/format_input.xls", formatting_info=True, on_demand=True)
outBook = xlutils.copy.copy(inBook)

outBook.get_sheet(0).write(0,0,'changed!')
outBook.save(r"path/to/file/format_output.xls")

在这里输入图像描述

EN

回答 1

Stack Overflow用户

发布于 2015-01-30 16:49:53

在使用openpyxl时,我遇到了类似的问题--由于某些原因,这在可用的模块中似乎处理得不太好。在输入数据之后,我只需根据需要重新设置单元格的样式,使用如下语法:

代码语言:javascript
复制
#Formatting
from openpyxl.styles import Style, Color, PatternFill, Alignment, Font, NumberFormat
#Allows for conditional formatting
from openpyxl.formatting import CellIsRule #Allows for Conditional Formatting

for cell in changed_cells:
    cell.style = Style(fill=PatternFill(patternType='solid', fgColor=Color('FFff8888')), 
                         font=Font(name="Arial",size=11), 
                         alignment=Alignment(horizontal="center"))

有关用xlrd实现这类事情的语法的信息可以找到这里

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28240254

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档