Python通过pywin32操作Excel

欢迎关注微信公众号“Python小灶,和我一起每天学习Python新知识”
我的Github地址
在这里插入图片描述

需求

Excel现在已经成为生活工作中不可缺少的软件之一,如果你经常使用此软件,或许已经被它各种各样高效的操作所折服。然鹅,我还是要告诉你,当Python遇上Excel,可以让Excel的效率提高不止100倍!!!

导航

​day01:简单认识Python和Excel,进行新建和打开表格的入门操作

day02:对表格进行基础的增删改查操作

day03:开始骚操作,

1、批量建表、删表、建sheets、删sheet、拆表、合表

2、讲解Python操作Excel的方法依据

day04: 这节课比较简单,主要是字体、单元格颜色等的设置

思路

目前可实现的对excel的操作的第三方库较多,本人用过的有openpyxl,不过本次系列教程用win32com演示,因为win32com支持的格式更为宽泛。

难度

★★★★

视频讲解

(欢迎来B站找我,“有只小新”,B站观看效果更佳,可以倍速观看)

https://www.bilibili.com/video/av711562274

搜索av711562274

或者长按二维码识别(想要二维码有这种效果,请关注公众号领取教程)
Python通过pywin32操作Excel_第1张图片

准备工作

1、Python+Pycharm社区版
2、pywin32(win32com)

Day01:​用Python创建和打开Excel表格

"""
-*- coding: utf-8 -*-
@Author  : blyang
@project : PythonCode
@Time    : 2020/7/1 23:21
"""
import os
import time
import math
import win32com.client as win32
​
excel = win32.Dispatch("Excel.Application")
excel.DisplayAlerts = False  # 关闭警告
excel.Visible = True  # 程序可见
​
pwd = os.getcwd()
# 新建一个表格
# wb = excel.Workbooks.Add()
# wb.SaveAs(pwd + "\\demo.xlsx")
# time.sleep(3)
# 打开一个已有的表格
excel.Workbooks.open(pwd + os.sep + "demo.xlsx")
# 额外示例
ws = excel.Worksheets("Sheet1")
for i in range(1, 101):
    ws.Cells(i, 1).Value = i
    ws.Cells(i, 2).Value = math.pow(i, 2)
time.sleep(5)
ws.SaveAs(pwd + os.sep + "demo.xlsx")
# 退出
excel.Quit()

Day02:​用Python操作Excel进行基础的增删改查

"""
-*- coding: utf-8 -*-
@Author  : blyang
@project : PythonCode
@Time    : 2020/7/28 23:19
"""
import os
import time
import win32com.client as win32

excel = win32.Dispatch("Excel.Application")
excel.DisplayAlerts = False  # 关闭警告
excel.Visible = True  # 程序可见

pwd = os.getcwd()
myexcel = excel.Workbooks.open(pwd + os.sep + "demo.xlsx")  # 打开一个已有的表格
mysheet = myexcel.Worksheets("Sheet1")  # 使sheet1处于工作状态
for i in range(1, 10):  # 增
    for j in range(1, i + 1):
        mysheet.Cells(i, j).Value = "{} * {} = {}".format(i, j, i * j)
        # time.sleep(0.1)

mysheet.Cells(1, 1).Value = None  # 删
# time.sleep(0.1)
mysheet.Range("A1:D5").Value = None

for i in range(1, 10):  # 查,改
    for j in range(1, i + 1):
        if mysheet.Cells(i, j).Value is not None:
            mysheet.Cells(i, j).Value = "Hello Python"
            # time.sleep(0.1)

mysheet.Range("A1:I5").Value = None
# time.sleep(0.1)

myexcel.SaveAs(pwd + os.sep + "demo.xlsx")  # 保存
excel.Quit()  # 退出

day03:1、批量建表、删表、建sheets、删sheet、拆表、合表,2、讲解Python操作Excel的方法依据

"""
-*- coding: utf-8 -*-
@Author  : blyang
@project : PythonCode
@Time    : 2020/8/5 22:43
"""
import os
import time
import win32com.client as win32

excel = win32.Dispatch("Excel.Application")
excel.DisplayAlerts = False  # 关闭警告
excel.Visible = True  # 程序可见

pwd = os.getcwd()
# 新建N个表格
# wb = excel.Workbooks.Add()
# for i in range(1, 15):
#     wb.SaveAs(pwd + os.sep + "8月{}号.xlsx".format(i))

# 删除N个表格
# for i in range(1, 15):
#     os.remove(pwd + os.sep + '8月{}号.xlsx'.format(i))

myexcel = excel.Workbooks.open(pwd + os.sep + "demo.xlsx")  # 打开一个已有的表格
# for i in range(1, 6):
#     newSheet = myexcel.Sheets.Add()
#     newSheet.Name = "8月{}号".format(i)

# for i in range(1, 6):
#     if i > 1:
#         newSheet = myexcel.Sheets.Add(Before=None, After=myexcel.Worksheets("8月{}号".format(i-1)))
#         newSheet.Name = "8月{}号".format(i)
#     else:
#         newSheet = myexcel.Sheets.Add(Before=None, After=myexcel.Worksheets("Sheet1"))
#         newSheet.Name = "8月{}号".format(i)

# 删除N个sheets
# for i in range(1, 6):
#     myexcel.Worksheets("8月{}号".format(i)).Delete()

# 激活工作表
# for i in range(1, 6):
#     myexcel.Worksheets("8月{}号".format(i)).Activate()
#     time.sleep(1)

# 移动工作表
# myexcel.Worksheets("8月3号").Move(Before=None, After=myexcel.Worksheets("Sheet1"))
# 拷贝移动
# myexcel.Worksheets("8月2号").Copy(Before=None, After=myexcel.Worksheets("Sheet1"))

# N个sheets拆分成独立的表格  VB代码演示
# for i in range(1, 6):
#     name = "8月{}号".format(i)
#     myexcel.Worksheets(name).Copy()
#     excel.ActiveWorkbook.SaveAs(pwd + os.sep + "{}.xlsx".format(name))
# excel.Workbooks.Close()

# N个独立的表格合成一个
for i in range(1, 6):
    name = "8月{}号".format(i)
    my_excel = excel.Workbooks.open(pwd + os.sep + "{}.xlsx".format(name))
    my_excel.Worksheets(name).Copy(Before=None, After=myexcel.Worksheets("Sheet1"))

myexcel.SaveAs(pwd + os.sep + "demo.xlsx")  # 保存
excel.Workbooks.Close()
excel.Quit()  # 退出

day04:这节课比较简单,主要是字体、单元格颜色等的设置

"""
-*- coding: utf-8 -*-
@Author  : blyang
@project : PythonCode
@Time    : 2020/8/17 22:59
"""
import os
import time
import random
import win32com.client as win32
​
excel = win32.Dispatch("Excel.Application")
excel.DisplayAlerts = False  # 关闭警告
excel.Visible = True  # 程序可见
​
pwd = os.getcwd()
# 打开一个已有的表格
myexcel = excel.Workbooks.open(pwd + os.sep + "demo.xlsx")# 选择指定工作表
# myexcel.Worksheets(2).Activate()
# mySheet = myexcel.Worksheets(2)
# # 设置单元格字体为红色
# mySheet.Cells(1, 1).Font.ColorIndex = 3
# time.sleep(1)
# # 设置单元格字体为粗体
# mySheet.Cells(1, 1).Font.Bold = True
# time.sleep(1)
# # 设置单元格字体
# mySheet.Cells(1, 1).Font.Name = "微软雅黑"
# time.sleep(1)
# # 设置单元格颜色
# # https://docs.microsoft.com/en-us/previous-versions/office/developer/office-2007/cc296089(v=office.12)?redirectedfrom=MSDN
# mySheet.Range("A2:A16").Interior.ColorIndex = 15# 一个很炫酷但是没鸟用的小拓展
myexcel.Worksheets(3).Activate()
mySheet = myexcel.Worksheets(3)
# for i in range(57):
#     if i > 54:
#         break
#     mySheet.Cells(1, 1).Interior.ColorIndex = i
#     time.sleep(0.3)
#     mySheet.Cells(1, 2).Interior.ColorIndex = i + 1
#     time.sleep(0.2)
#     mySheet.Cells(1, 3).Interior.ColorIndex = i + 2
#     time.sleep(0.1)
​
​
# while True:
#     for i in range(1, 10):
#         for j in range(1, 10):
#             mySheet.Cells(i, j).Interior.ColorIndex = random.randint(0, 56)
#         time.sleep(0.1)
​
myexcel.SaveAs(pwd + os.sep + "demo.xlsx")
# 退出
excel.Quit()

你可能感兴趣的:(python,Excel,pywin32,python,excel,编程语言)