栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

通过openpyxl改变excel文字大小

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

通过openpyxl改变excel文字大小

#单个单元格调整格式

from openpyxl.styles import Font
import os
import openpyxl
pth = r'kevin_folderPythonexcel'
name = '调整格式.xlsx'
file = os.path.join(pth,name)
wb = openpyxl.load_workbook(file)
wb.sheetnames
ws = wb['Sheet1']
fontStyle = Font(size = "20")
#print(ws.cell(row = 1, column = 1))
ws.cell(row = 1, column = 1).font = fontStyle #, value = 'Test Text'
vlue = 'Test Text'
ws.cell(row = 1, column = 1,value =vlue)
newname = '调整格式后.xlsx'
newefile = os.path.join(pth,newname)
wb.save(newefile)

##调整前文字大小

#调整后文字大小 

#区域调整格式
from openpyxl.styles import Font
import os
from openpyxl.utils.cell import get_column_letter
import openpyxl
pth = r'kevin_folderPythonexcel'
name = '调整格式.xlsx'
file = os.path.join(pth,name)
wb = openpyxl.load_workbook(file)
wb.sheetnames
ws = wb['Sheet1']
fontStyle = Font(size = "20")
maxRows = ws.max_row
maxColumns = ws.max_column
for i in range(maxColumns):
#    print(i)
    for k in range(maxRows):
#        print(k)
#        print(get_column_letter(i+1))
#        cell = str(ws[get_column_letter(i+1)+str(k+1)].value)
        cell = ws[get_column_letter(i+1)+str(k+1)]
#        print(cell)
        cell.font = fontStyle

newname = '调整格式后区域.xlsx'
newefile = os.path.join(pth,newname)
wb.save(newefile)

#另外一种方法

#区域调整另外一种方法

#区域调整格式
from openpyxl.styles import Font
import os
from openpyxl.utils.cell import get_column_letter
import openpyxl
pth = r'kevin_folderPythonexcel'
name = '调整格式.xlsx'
file = os.path.join(pth,name)
wb = openpyxl.load_workbook(file)
wb.sheetnames
ws = wb['Sheet1']
fontStyle = Font(size = "20")
maxRows = ws.max_row
maxColumns = ws.max_column
for i in range(1,maxColumns+1):
#    print(i)
    for k in range(1,maxRows+1):
#        print(k)
#        print(get_column_letter(i+1))
#        cell = str(ws[get_column_letter(i+1)+str(k+1)].value)
        cell = ws.cell(row = k,column=i)
        print(cell)
#        print(cell)
        cell.font = fontStyle

newname = '调整格式后区域other方法.xlsx'
newefile = os.path.join(pth,newname)
wb.save(newefile)

#调整后文字大小

#统一增加一列

from openpyxl.styles import Font
import os
from openpyxl.utils.cell import get_column_letter
import openpyxl
pth = r'kevin_folderPythonexcel'
name = '调整格式.xlsx'
file = os.path.join(pth,name)
wb = openpyxl.load_workbook(file)
wb.sheetnames
ws = wb['Sheet1']
fontStyle = Font(size = "20")
maxRows = ws.max_row
maxColumns = ws.max_column
for i in range(maxColumns):
#    print(i)
    for k in range(1,maxRows):
#        print(k)
#        print(get_column_letter(i+1))
        cell2 = str(ws[get_column_letter(i+1)+str(k+1)].value)
        cell = ws[get_column_letter(i+1)+str(k+1)]
        newCell = "'=,"+cell2[0:]

        ws['D'+str(k+1)]=newCell
#        ws[get_column_letter(i+1)+str(k+1)]=newCell
#        print(cell)
        cell.font = fontStyle

newname = '调整格式后区域3.xlsx'
newefile = os.path.join(pth,newname)
wb.save(newefile)

#结果:

注:调整excel文件内单元格格式可以先定格式,然后遍历所有的单元格进行调整

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/983428.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号