python之excel文件打印设置,及单元格样式修改

#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@文件    :nclass_score_5terms.py
@说明    :excel文件单元格格式批量修改
xlrd读,xlwt写,xlutils作为两者的桥梁,将读到的xlrd转换为xlwt
经查阅资料xlwt好像是在写入的同时设置字体样式大小等:new_sheet.write(2, 1, 12, style)  # 在2行1列写入数据,style为我们设置好的格式
这里使用openpyxl设置单元格样式,注意openpyxl只能操作xlsx文件,xls不适用。可以手动提前另存为xlsx文件,或者使用使用pywin32模块进行转换
1、xlrd打开文件
2、利用xlutils.copy将xlrd对象拷贝转化为xlwt对象
3、使用xlwt设置横向打印,页边距,行高列宽
4、将xls文件转换为xlsx文件
5、使用openpyxl设置字体及字号,标题行取消框线
@时间    :2021/06/10 09:13:26
@作者    :侃侃
@版本    :3.8
'''
import xlrd
import xlwt
from xlutils.copy import copy

from openpyxl.styles import Font
from openpyxl import load_workbook

import os
import win32com.client as win32

from openpyxl.styles import Font
from openpyxl import load_workbook
from openpyxl.styles import Border,Side

#将指定文件设置为横向打印,及设置页边距,行高列宽
def xlwt_set(filepath,newfilepath):
    
    workbook = xlrd.open_workbook(filepath, formatting_info=True)  # 打开工作簿

    sheets=workbook.sheets()
    ncols = sheets[0].ncols#获取列数

    new_workbook = copy(workbook)  # 将xlrd对象拷贝转化为xlwt对象
    sheet1=new_workbook.get_sheet(0)#读取第一张表格
    # 设置页眉为空
    sheet1.set_header_str(''.encode())
    # 设置页脚为空
    sheet1.set_footer_str(''.encode())
    #设置表格数据打印水平不居中(默认居中)
    #sheet1.set_print_centered_horz(0)
    #设置表格数据打印垂直居中(默认不居中)
    #sheet1.set_print_centered_vert(1)

    sheet1.set_portrait(0)#设置横向打印
    sheet1.set_top_margin(2)#设置上页边距
    sheet1.col(0).width = 4000#设置学号列宽
    sheet1.col(1).width = 3000#设置姓名列宽
    sheet1.row(2).height= 3200#设置第二行行高

    # sheet1.set_bottom_margin(0)
    # sheet1.set_left_margin(0)
    # sheet1.set_right_margin(0.55)

    #设置课程列宽
    for i in range(2,ncols):
        sheet1.col(i).width = 1700

    new_workbook.save(newfilepath)

#多个filepath路径文件,使用遍历
def nxlwt_set(dir):
    for root_dir,sub_dir,files in os.walk(r'' + dir):#遍历目录下的根目录,子目录,所有文件
        # 对文件列表中的每一个文件进行处理,如果文件名字是以‘xlxs’结尾就
        # 认定为是一个excel文件,当然这里还可以用其他手段判断,比如你的excel
        # 文件名中均包含‘res’,那么if条件可以改写为
        # if file.endswith('xlsx') and 'res' in file:
        for file in files:
            filepath=dir+"\\"+file
            newfilepath=dir+"\\new"+file
            xlwt_set(filepath,newfilepath)

#将指定文件夹下的xls文件转化为xlsx文件
def nxls_toxlsx(dir):
     
    for root_dir,sub_dir,files in os.walk(r'' + dir):#遍历目录下的根目录,子目录,所有文件
        # 对文件列表中的每一个文件进行处理,如果文件名字是以‘xlxs’结尾就
        # 认定为是一个excel文件,当然这里还可以用其他手段判断,比如你的excel
        # 文件名中均包含‘res’,那么if条件可以改写为
        # if file.endswith('xlsx') and 'res' in file:
        for file in files:
            filepath=dir+"\\"+file
            #xls转换为xlsx
            #filename = 'C:\\Users\\lenovo\\Desktop\\班级成绩单打印\\002.xls'
            Excelapp = win32.gencache.EnsureDispatch('Excel.Application')
            workbook = Excelapp.Workbooks.Open(filepath)
            # 转xlsx时: FileFormat=51,
            # 转xls时:  FileFormat=56,
            workbook.SaveAs(filepath.replace('xls', 'xlsx'), FileFormat=51)
            workbook.Close()
            Excelapp.Application.Quit()
            # 删除源文件
            os.remove(filepath)

            # 如果想将xlsx的文件转换为xls的话,则可以使用以下的代码:
            # workbook.SaveAs(filename.replace('xlsx', 'xls'), FileFormat=56)
#openpyxl设置字体及字号,标题行取消框线
def openpyxl_set(filepath,newfilepath):
    wb = load_workbook(filepath)
    sheet = wb.active

    sheet.cell(row=1, column=1).font=Font(name="宋体",size=14)#设置标题行字体及大小
    sheet.row_dimensions[1].height=30#设置标题行行高
    font = Font(name="宋体",size=12)#font = Font(name="宋体",size=12,bold=False,italic=False,color="59A869")

    #标题行去掉框线
    border = Border(left=Side(border_style=None,color='000000'),right=Side(border_style=None,color='000000'),top=Side(border_style=None,color='000000'))

    for i in range(1,sheet.max_column+1):
        sheet.cell(row=1,column=i).border = border

    for i in range(2,sheet.max_row+1):
        for j in range(1,sheet.max_column+1):
            sheet.cell(row=i, column=j).font=font
    wb.save(newfilepath)

#多个filepath路径文件,使用遍历
def nopenpyxl_set(dir):
    for root_dir,sub_dir,files in os.walk(r'' + dir):#遍历目录下的根目录,子目录,所有文件
        # 对文件列表中的每一个文件进行处理,如果文件名字是以‘xlxs’结尾就
        # 认定为是一个excel文件,当然这里还可以用其他手段判断,比如你的excel
        # 文件名中均包含‘res’,那么if条件可以改写为
        # if file.endswith('xlsx') and 'res' in file:
        for file in files:
            filepath=dir+"\\"+file
            newfilepath=dir+"\\new"+file
            openpyxl_set(filepath,newfilepath)


# nxlwt_set('C:\\Users\\lenovo\\Desktop\\test\\新建文件夹')
# nxls_toxlsx('C:\\Users\\lenovo\\Desktop\\test\\新建文件夹')
nopenpyxl_set('C:\\Users\\lenovo\\Desktop\\test\\新建文件夹\\新建文件夹')

 

  • 3
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值