python应用异常解决:sheet.column_dimensions[‘C‘].font=italic24Font无效

news/2024/7/21 6:51:48 标签: bug, excel, python

pythonsheetcolumn_dimensionsDfontitalic24Font_0">python应用异常解决:sheet.column_dimensions[‘D’].font=italic24Font无效


文章目录

  • python应用异常解决:sheet.column_dimensions['D'].font=italic24Font无效
  • 前言
  • 一、问题引入
  • 二、解决问题
    • 1.用cell来解决
    • 2.获取结果
  • 总结


前言

本来想用sheet.column_dimensions[‘D’].font=italic24Font,给Excel的第D列进行相应的格式调整,谁知道以及输入的字符,好像并没有进行相应的格式设置。


一、问题引入

先用sheet.column_dimensions[‘D’].font=italic24Font

这是原本的Excel。开始修改格式。
在这里插入图片描述

代码如下

python">import openpyxl,os
from openpyxl.styles import Font
os.chdir(r'C:\Users\LX\Desktop')
wb=openpyxl.load_workbook('LOVE.xlsx')
#sheet=wb['ABC']
sheet=wb.active
italic24Font=Font(size=24,italic=True)
sheet.column_dimensions['D'].font=italic24Font
##for cell in sheet['D']:
##    cell.font=italic24Font

sheet['B3'].font=italic24Font
#styleObj=Font(font=italic24Font)
#sheet['A'].style/styleObj
wb.save('LOVE.xlsx')

来看一下结果,发现D行中并没有改变格式,但是在还没有输入的单元格中再输入相应的文字时,可以发现,格式却改变了,对于如果需要用python办公的高效人员来说,这无疑是一个又鸡肋又烦恼的打击,想想我还不如直接用Excel算了。那么我们来解决!
在这里插入图片描述
在这里插入图片描述

二、解决问题

1.用cell来解决

python">import openpyxl,os
from openpyxl.styles import Font
os.chdir(r'C:\Users\LX\Desktop')
wb=openpyxl.load_workbook('LOVE.xlsx')
#sheet=wb['ABC']
sheet=wb.active
italic24Font=Font(size=24,italic=True)
#sheet.column_dimensions['D'].font=italic24Font
for cell in sheet['D']:
    cell.font=italic24Font

sheet['B3'].font=italic24Font
#styleObj=Font(font=italic24Font)
#sheet['A'].style/styleObj
wb.save('LOVE.xlsx')

其他一样,重点在于:
for cell in sheet[‘D’]:
cell.font=italic24Font

2.获取结果

python">for cell in sheet['D']:
    cell.font=italic24Font

在这里插入图片描述

修改之后,成功搞定了,D列的格式设置,我们常用sheet.column_dimensions[‘D’]来调整宽度,如sheet.column_dimensions[‘D’].width=60(上图)
在这里插入图片描述
可以看见,对宽度的调整,一次成功。


总结

如果要修改已经有输入字符串的单元列格式,用cell代替sheet.column_dimensions来使用,可以解决你的困扰!


http://www.niftyadmin.cn/n/773180.html

相关文章

Python与Excel的各类方法使用【细致一行一行来】

Python与Excel的各类方法使用【一行一行来】 文章目录Python与Excel的各类方法使用【一行一行来】当下实用代码openpyxl更新当下实用代码 不急,跟着我一行一行来看! import openpyxl,os from openpyxl.styles import Font os.chdir(rC:\Users\LX\Desktop…

Python Error解决方案:from aip import AipOcr ImportError: cannot import name ‘AipOcr‘ from ‘aip‘

Python Error解决方案:from aip import AipOcr ImportError: cannot import name ‘AipOcr’ from ‘aip’ 文章目录Python Error解决方案:from aip import AipOcr ImportError: cannot import name AipOcr from aip前言一、先卸载aip二、再卸载baidu-ai…

用两个栈模拟队列

第一个栈负责进队&#xff0c;第二个负责出队 用两个栈模拟队列 #include<stdio.h> #define maxsize 10 typedef struct s {int data[maxsize];int top; }SqStack; int InitStack(SqStack& S1) {S1.top -1;return 1; } int push(SqStack& S, int x) { S.data[S…

python:三个你必须记住的pyinstaller打包命令

python-pyinstaller打包&#xff1a;三个你必须记住的命令 文章目录python-pyinstaller打包&#xff1a;三个你必须记住的命令前言一、pyinstaller打包机制二、生成EXE1.-F生成exe2.-F -w生成exe3.-F -w -i生成exe&#xff08;ico改图标&#xff09;前言 今天我们来谈谈用pyin…

完数C语言

/*一个数如果恰好等于它的因子之和&#xff0c;这个数就称为完数。 例如&#xff0c;6的因子为1&#xff0c;2&#xff0c;3&#xff0c;而6123&#xff0c;因此6是完数&#xff0c;输出1000以内的完数。*/ #include<stdio.h> int main() {int i, j, sum 0, l 0;int a[…

一个链接搞定你要的打包ico图标

迅速导出ico图标做开发 今天一个链接搞定&#xff01;我觉得比较好用&#xff01; 文章目录迅速导出ico图标做开发http://www.ico51.cn/ 这个链接非常方便有效&#xff0c;希望可以帮到大家&#xff01;&#xff01;&#xff01;

python安装cv2

pip install opencv-python(慢) pip install -i https://pypi.tuna.tsinghua.edu.cn/simple opencv-python&#xff08;快&#xff09;

4行python代码做出打字机效果

4行python代码做出打字机效果 文章目录4行python代码做出打字机效果前言一、导入库二、完整代码前言 今天想用python做一个打字机的效果出来看看&#xff01; 一、导入库 import pyautogui,time二、完整代码 import pyautogui,time time.sleep (5) #留出反应时间 pya…