通过openpyxl改变excel文字大小

#单个单元格调整格式

from openpyxl.styles import Font
import os
import openpyxl
pth = r'\kevin_folder\Python\excel'
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_folder\Python\excel'
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_folder\Python\excel'
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_folder\Python\excel'
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文件内单元格格式可以先定格式,然后遍历所有的单元格进行调整


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部