import os, openpyxl
from openpyxl.styles import Font, Style
os.chdir("C:\\")
wb = openpyxl.Workbook()
sheet = wb['Sheet']
italic24Font = Font(size = 24, italic = True)
styleObj = Style(font = italic24Font)
sheet['A'].style/styleObj
sheet['A1'] = 'Hello world!'
wb.save('styled.xlsx')

自学《Python编程快速上手》P232的内容,运行下面代码时,程序报错

问题分析: openpyxl的后续版本有所修改,此外,《Python编程快速上手》的原著作者后续也已对该内容做了修改。

见链接:https://automatetheboringstuff.com/chapter12/

正确代码:

 import os, openpyxl
from openpyxl.styles import Font
os.chdir("C:\\")
wb = openpyxl.Workbook()
sheet = wb['Sheet']
italic24Font = Font(size = 24, italic = True)
sheet['A1'].font = italic24Font
sheet['A1'] = 'Hello world!'
wb.save('styled.xlsx')

补充: 如果要对A列进行格式设置,则代码书写为:sheet.column_dimensions['A'].font = italic24Font。参见链接:点击打开链接

python: ImportError: cannot import name 'Style' from 'openpyxl.styles' 解决方法的更多相关文章

  1. Python提示AttributeError 或者DeprecationWarning: This module was deprecated解决方法

    Python提示AttributeError 或者DeprecationWarning: This module was deprecated解决方法 在使用Python的sklearn库时,发现sk ...

  2. js中style.display=""无效的解决方法

    本文实例讲述了js中style.display=""无效的解决方法.分享给大家供大家参考.具体解决方法如下: 一.问题描述: 在js中我们有时想动态的控制一个div显示或隐藏或更多 ...

  3. Tex: The top-level auxiliary file: *.aux I couldn't open style file IEEEtran.bst 解决方法

    参考: Bibliography is not printed using Kile on Ubuntu Tex: The top-level auxiliary file: *.aux I coul ...

  4. Sublime2编译Python程序EOFError:EOF when reading a line解决方法【转】

    在Sublime2中编译运行Python文件时,如果代码中包含用户输入的函数时(eg. raw_input()),Ctrl+b编译运行之后会提示以下错误: 解决方法:安装SublimeREPL打开Su ...

  5. php Yaf_Loader::import引入文件报错的解决方法

    php Yaf_Loader::import引入文件报错的解决方法 改下配置文件就行<pre>yaf.use_spl_autoload=1</pre> 也可以PHP动态修改 毕 ...

  6. python scrapy cannot import name xmlrpc_client的解决方案,解决办法

    安装scrapy的时候遇到如下错误的解决办法: "python scrapy cannot import name xmlrpc_client" 先执行 sudo pip unin ...

  7. [搬砖]Pycharm中启动IPython notebook失败提示load_entry_point ImportError: Entry point ('console_scripts', 'ipython') not found的解决方法

    前提:直接运行ipython正常,“which -a ipython”命令显示也只有一个ipython存在,在ipynb文件中点运行启动notebook时提示错误类似如下: Traceback (mo ...

  8. Python开发:部分第三方库无法在线安装解决方法

    前言:Python开发:Python2和Python3的共存和切换使用 一.问题如下: 1.截图: 2.错误信息: Could not find a version that satisfies th ...

  9. python logging 日志轮转文件不删除问题的解决方法

    项目使用了 logging 的 TimedRotatingFileHandler : #!/user/bin/env python # -*- coding: utf-8 -*- import log ...

随机推荐

  1. zabbix监控托管主机遇到问题

    昨天监控公司的托管主机时发现监控不上,回想起来其实就是个小问题,分分钟能解决的事,排错的过程才是真正耗心费神的. 监控环境: A zabbix server: 192.168.17.110 serve ...

  2. MySql left join 多表连接查询优化语句

    先过滤条件然后再根据表连接 同时在表中建立相关查询字段的索引这样在大数据多表联合查询的情况下速度相当快 创建索引: create index ix_register_year ON dbo.selec ...

  3. FastList使用

    之前使用的组件是ListView,当时要添加一个下拉刷新,上拉加载的功能,所以对ListView做了一些封装,但是后来看官方文档,不建议再使用ListView,因为效率问题,做过Android的朋友都 ...

  4. Java基础知识(JAVA之泛型)

    什么是泛型?为什么要使用泛型? 泛型,即“参数化类型”.一提到参数,最熟悉的就是定义方法时有形参,然后调用此方法时传递实参.那么参数化类型怎么理解呢?顾名思义,就是将类型由原来的具体的类型参数化,类似 ...

  5. poj3126

    被坑了3个小时,本来以为算法错了,谁知道,竟然是素数筛弄错了 !!! #include <iostream>#include <stdio.h>#include <str ...

  6. 【LeetCode每天一题】Pow(x, n)(平方)

    Implement pow(x, n), which calculates x raised to the power n (x,n). Example 1:                 Inpu ...

  7. [LeetCode] 610. Triangle Judgement_Easy tag: SQL

    A pupil Tim gets homework to identify whether three line segments could possibly form a triangle. Ho ...

  8. [LeetCode] 762. Prime Number of Set Bits in Binary Representation_Easy

    Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime ...

  9. np.percentile()

    np.percentile(a, q, axis=None, out=None, overwrite_input=False, interpolation='linear', keepdims=Fal ...

  10. .Net拾忆:从List去除重复-拾忆集合

    方法1: private static List<int> DistinctList(List<int> list) {//去除重复 HashSet<int> ha ...