1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > python之excel文件打印设置 及单元格样式修改

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

时间:2019-05-11 17:20:10

相关推荐

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设置字体及字号,标题行取消框线@时间 :/06/10 09:13:26@作者 :侃侃@版本 :3.8'''import xlrdimport xlwtfrom xlutils.copy import copyfrom openpyxl.styles import Fontfrom openpyxl import load_workbookimport osimport win32com.client as win32from openpyxl.styles import Fontfrom openpyxl import load_workbookfrom 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 = 1700new_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+"\\"+filenewfilepath=dir+"\\new"+filexlwt_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.activesheet.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 = borderfor i in range(2,sheet.max_row+1):for j in range(1,sheet.max_column+1):sheet.cell(row=i, column=j).font=fontwb.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+"\\"+filenewfilepath=dir+"\\new"+fileopenpyxl_set(filepath,newfilepath)# nxlwt_set('C:\\Users\\lenovo\\Desktop\\test\\新建文件夹')# nxls_toxlsx('C:\\Users\\lenovo\\Desktop\\test\\新建文件夹')nopenpyxl_set('C:\\Users\\lenovo\\Desktop\\test\\新建文件夹\\新建文件夹')

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。