首页 > 编程语言 >python openpyxl 操作excel

python openpyxl 操作excel

时间:2023-05-15 21:11:08浏览次数:35  
标签:style sheet openpyxl python excel cell font border Side

 

openpyxl 是从1开始计数,遍历操作时请用1为起始点,且总数加一,例:for i in range(1, n+1)

 

单元格样式

所需导入模块

from openpyxl.styles import Alignment, Font, Border, Side,PatternFill

样式

    # 设置边框线
    align = Alignment(horizontal='center', vertical='center', wrap_text=True)
    # 设置边框线样式
    top = Side(border_style='thin', color='000000')
    bottom = Side(border_style='thin', color='000000')
    left = Side(border_style='thin', color='000000')
    right = Side(border_style='thin', color='000000')
    border = Border(top=top, bottom=bottom, left=left, right=right)
    # 设置字体
    font = Font(u'宋体', size='10.5')
    # 合并单元格 所输行列是会一起包含合并
    # sheet.merge_cells("A1:A3")
    # sheet.merge_cells(start_row=1, end_row=3, start_column=10, end_column=11)
    # 设置填充颜色
    fille = PatternFill('solid', fgColor="FF00FF")

单元格设置

        # 字体
        # sheet.cell(i, 4).font = font
        # sheet[f'D{i}'].font = font

        # 填充颜色
        # sheet.[f'D{i}'].fill = fille
        # sheet.cell(1, 1, "洋红色").fill = fille

        # 对齐方式
        # sheet.cell(i, 4).alignment = align
        # sheet[f'D{i}'].alignment = align

        # 边框
        # sheet.cell(i, 6).border = border
        # sheet[f'E{i}'].border = border

        # 行高
        # sheet.row_dimensions[i + 1].height = cell_height
        # 列宽 i为必须字母,不能是数字
        # sheet.column_dimensions[i].width = 20

 

标签:style,sheet,openpyxl,python,excel,cell,font,border,Side
From: https://www.cnblogs.com/lld76/p/17403067.html

相关文章

  • 粒子群算法PSO优化随机森林RFR的回归预测MATLAB代码 代码注释清楚,可以读取EXCEL数
    粒子群算法PSO优化随机森林RFR的回归预测MATLAB代码代码注释清楚,可以读取EXCEL数据,使用换自己数据集。很方便,初学者容易上手。YID:8945655042973482......
  • SICP:元循环求值器(Python实现)
    求值器完整实现代码我已经上传到了GitHub仓库:TinySCM,感兴趣的童鞋可以前往查看。这里顺便强烈推荐UCBerkeley的同名课程CS61A。在这个层次结构的最底层是对象语言。对象语言只涉及特定的域,而不涉及对象语言本身(比如它们的文法规则,或其中的其体句子)。如要涉及它们,则要有一种元......
  • Python基础语法入门
    Python基础语法入门1、Python的注释符号1、什么是注释?#学习任何一门代码,先学注释,注释是代码之母注释就是对一段代码的解释说明,它不会参与到代码的运行,只是起到提示作用2、如何注释?2.1、#单行注释#它可以使用快捷键帮助我们把代码写的更加规范快捷键:Ctrl+alt+1(格式......
  • 了解Python的基本数据类型
    引入 我们学习变量是为了让计算机能够记忆事物的某种状态,而变量的值就是用来存储事物状态的,很明显事物的状态是分成不同种类的(例如水的液态,气态和固态),所以变量值也应该有不同的类型 一、数字类型int(整型)float(浮点型)不可变数据类型int,整型,是没有小数点的数字......
  • < Python全景系列-2 > Python数据类型大盘点
    <Python全景系列-2>Python数据类型大盘点欢迎来到我们的系列博客《Python全景系列》!在这个系列中,我们将带领你从Python的基础知识开始,一步步深入到高级话题,帮助你掌握这门强大而灵活的编程语法。无论你是编程新手,还是有一定基础的开发者,这个系列都将提供你需要的知识和技能。Py......
  • python基础学习-读写CSV文件
    CSV文件介绍参考:Python-Core-50-Courses/第23课:用Python读写CSV文件.mdatmaster·jackfrued/Python-Core-50-Courses(github.com)CSV 全称逗号分隔值文件是一种简单、通用的文件格式,被广泛的应用于应用程序(数据库、电子表格等)数据的导入和导出以及异构系统之间的数据......
  • python基础学习-用Python操作Word和PowerPoint
    参考链接:Python-Core-50-Courses/第26课:用Python操作Word文件和PowerPoint.mdatmaster·jackfrued/Python-Core-50-Courses(github.com)......
  • python基础学习-用Python读写Excel文件
    参考链接:Python-Core-50-Courses/第24课:用Python读写Excel文件-1.mdatmaster·jackfrued/Python-Core-50-Courses(github.com)Python-Core-50-Courses/第25课:用Python读写Excel文件-2.mdatmaster·jackfrued/Python-Core-50-Courses(github.com)......
  • Excel-win32com的宏操作
    importwin32com.clientxl=win32com.client.Dispatch("Excel.Application")#实例化Excel应用程序wb=xl.Workbooks.Open(r'C:\Users\Desktop\test1.xlsm')xl.Application.Run('test1.xlsm!模块1.getTime2("现在时刻")')wb.Save()xl.Appl......
  • 有翅膀Excel-可重新分发的主互操作程序集
    usingMicrosoft.Vbe.Interop;usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Diagnostics;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.W......