• excel表自动化-python


    1.replace 替换函数
    a = "abcd"
    b = "abc"
    c = a.replace(b,'0') # 把abc替换成0
    print(c)

    2.插入内容%s
    b = 'aaa'
    a = "ab%scd"%b #%s是插入的位置,后面的%是插入的内容
    print(a)
    或者用format:
    a = "ab{}cd".format('00')

    3.切片
    b = "abcdefghi"
    print(b[2:4]) #切 cd 从0开始数器
    print(b[-3:]) #倒着数3个 ghi

    4.列表的增删
    列表.append(“aaa”)
    列表.remove(“aaa”)

    5.for的使用
    a = [1,2,3,4,5,6]
    for i in a:
    print(i)

    6.其他常见函数
    range(1,100) #1数到100
    type("a") #类型print(type("d")) ---><class 'str'>
    str(1) #强制类型转换
    len("abc") #长度
    round(1.234,2) #1.234 保留两位小数
    a=input('请输入a的值') #输入

    7.自定义函数
    def Aaa(a,b,c):
    area = a*b*c
    print(area)
    Aaa(1,2,3)

    8.安装第三方库
    cmd中:pip instal 库名
    import xlrd #使用库

    9.excel表的读取
    使用r1c1 样式
    import xlrd
    xlsx1 = xlrd.open_workbook('F:供电局办公资料考勤测试.xlsx') # 打开工作簿
    table = xlsx1.sheet_by_index(0) # 打开第一个工作表
    # table1 = xlsx1.sheet_by_name("工作表名字") 或者这种方法打开工作表
    print(table.cell_value(1,0)) #读取表格信息
    print(table.cell(1,0).value)
    print(table.row(1)[0].value) #功能一样,都是读取

    10.excel表新建
    import xlwt
    new_workbook = xlwt.Workbook() # 新建一个工作簿
    worksheet = new_workbook.add_sheet('new_test') # 新建一个工作表
    worksheet.write(0,1,"tesd")
    new_workbook.save("d:/test1.xls")

    11.利用excel模板写入
    from xlutils.copy import copy #用的其中的copy功能
    import xlrd
    import xlwt

    tem_excel = xlrd.open_workbook('D:/test.xls',formatting_info=True) #带格式打开
    tem_sheet = tem_excel.sheet_by_index(0)

    new_excel = copy(tem_excel) #复制一个模板
    new_sheet = new_excel.get_sheet(0) #获取第1个表
    new_sheet.write(1,3,100)
    new_sheet.write(2,3,200)
    new_excel.save('D:/test11.xls')

  • 相关阅读:
    GDI+小例子
    GDI & GDI+
    GDI绘图中的映射模式CDC::SetMapMode()
    Socket心跳包机制
    Winpcap网络开发库入门
    AdjustTokenPrivileges启用权限
    SetLocalTime设置本地时间
    UDP收/发广播包原理及步骤
    如何使用UDP进行跨网段广播
    Windows关机过程分析与快速关机
  • 原文地址:https://www.cnblogs.com/trevain/p/14120242.html
Copyright © 2020-2023  润新知