from tkinter import Tk
from time import sleep
from tkinter.messagebox import showwarning # 用于弹出窗口
import win32com.client as win32 # 安装模块 pypiwin32
warn=lambda app:showwarning(app,'即将退出进程')
Range=range(3,8)
def excel():
app='Excel'
xl=win32.gencache.EnsureDispatch('%s.Application'%app) # 调度 excel 进程
ss=xl.Workbooks.Add() # 添加一个工作表
sh=ss.ActiveSheet # 获取一个活动的工作表
xl.Visible=True # 显示电子表格
sleep(1)
sh.Cells(1,1).Value='python -to-%s Demo'%app # 在(1,1)(行,列)即 A1 写入内容
sleep(1)
for i in Range: # 循环在(i行,1列)中写入内容
sh.Cells(i,1).Value='Line %d'%i
sleep(1)
sh.Cells(i+2,1).Value="Th-th-th-that's all folks!"
warn(app) # 弹出退出警告框
ss.Close(False) # 不保存并关闭应用
xl.Application.Quit() # 退出应用
if __name__=='__main__':
Tk().withdraw() # 使用 withdraw() 方法不让 tk 顶级窗口出现
# 如果没有事先初始化顶级窗口,那么 Tk 会为你自动创建一个,而且不会将其隐藏,这样会在你的屏幕上造成一定的干扰。
excel()
from openpyxl import load_workbook
workbook = load_workbook(filename = "test.xlsx") # 打开并获取表格
print(workbook.sheetnames) # 获取并查看表格内sheet名
from openpyxl import load_workbook
workbook = load_workbook(filename = "test.xlsx") # 打开并获取表格
# workbook.sheetnames # 获取并查看表格内sheet名
sheet = workbook["Sheet1"] # 获取名为"Sheet1"的工作表
# print(sheet)
from openpyxl import load_workbook
workbook = load_workbook(filename = "test.xlsx") # 打开并获取表格
# workbook.sheetnames # 获取并查看表格内sheet名
sheet = workbook["Sheet1"] # 获取名为"Sheet1"的工作表
print(sheet.dimensions) # 获取表格的尺寸大小
A2:D29 # 表示该工作表从A2开始,到D29结束;
from openpyxl import load_workbook
workbook = load_workbook(filename = "test.xlsx") # 打开并获取表格
sheet = workbook.active # 获取当前活动的sheet
# print(sheet)
cell1 = sheet["A1"] # 查看当前sheet中A1格子的内容
cell2 = sheet["C11"] # 查看当前sheet中C11格子的内容
print(cell1.value, cell2.value)
from openpyxl import load_workbook
workbook = load_workbook(filename = "test.xlsx") # 打开并获取表格
sheet = workbook.active # 获取当前活动的sheet
print(sheet)
cell1 = sheet.cell(row = 1,column = 1) # 查看当前sheet中1行1列格子的内容
cell2 = sheet.cell(row = 11,column = 3) # 查看当前sheet中11行3列格子的内容
print(cell1.value, cell2.value)
from openpyxl import load_workbook
workbook = load_workbook(filename = "test.xlsx") # 打开并获取表格
sheet = workbook.active # 获取当前活动的sheet
print(sheet)
cell1 = sheet["A1"]
cell2 = sheet["C11"]
print(cell1.value, cell1.row, cell1.column, cell1.coordinate)
print(cell2.value, cell2.row, cell2.column, cell2.coordinate)
"""
.row 获取某个格子的行数;
.columns 获取某个格子的列数;
.corordinate 获取某个格子的坐标;
"""
方式一:
from openpyxl import load_workbook
workbook = load_workbook(filename = "test.xlsx") # 打开并获取表格
sheet = workbook.active # 获取当前活动的sheet
print(sheet)
# 获取A1到C2区域的值,按行获取,即A1,B1,C1,A2,B2,C2共6个单元格
cell = sheet["A1:C2"]
print(cell)
for i in cell: # 循环打印内容
for j in i:
print(j.value)
其他获取方式:
sheet["A"] --- 获取A列的数据
sheet["A:C"] --- 获取A,B,C三列的数据
sheet[5] --- 只获取第5行的数据
方式二:
from openpyxl import load_workbook
workbook = load_workbook(filename = "test.xlsx") # 打开并获取表格
sheet = workbook.active # 获取当前活动的sheet
print(sheet)
# 按行获取值
for i in sheet.iter_rows(min_row=2, max_row=5, min_col=1, max_col=2): # 获取行2到行5,列1到列2内的所有内容,并以行顺序读取数据
for j in i:
print(j.value)
# 按列获取值
for i in sheet.iter_cols(min_row=2, max_row=5, min_col=1, max_col=2): # 获取行2到行5,列1到列2内的所有内容,并以列顺序读取数据
for j in i:
print(j.value)
方式三:按行获取所有数据
from openpyxl import load_workbook
workbook = load_workbook(filename = "test.xlsx") # 打开并获取表格
sheet = workbook.active # 获取当前活动的sheet
print(sheet)
for i in sheet.rows: # 按行获取所有数据
print(i)
row_max_num= sheet.max_row # 取工作表最大列数
# print(row_max_num)
outputCommandList = []
for row in range(4, row_max_num + 1):
# item = {}
value1 = sheet.cell(row, 2).value
# item[key] = sheet.cell(row, 2).value
# if row != 1:
if value1 == '以下空白':
break
outputCommandList.append(value1)
# print(commandList)
from openpyxl import load_workbook
workbook = load_workbook(filename = "test.xlsx") # 打开并获取表格
sheet = workbook.active # 获取当前活动的sheet
print(sheet)
sheet["A1"] = "2333" # 写入A1单元格内容
# 写入A1单元格内容的另一种方式
cell = sheet["A1"]
cell.value = "2333"
workbook.save(filename = "newExcelFile.xlsx") # 以特定名称保存excel文件
from openpyxl import load_workbook
workbook = load_workbook(filename = "test.xlsx") # 打开并获取表格
sheet = workbook.active # 获取当前活动的sheet
print(sheet)
data = [
["唐僧","男","180cm"],
["孙悟空","男","188cm"],
["猪八戒","男","175cm"],
["沙僧","男","176cm"],
]
for row in data: # 会在表格已有的数据后面,插入这些数(按行插入)
sheet.append(row)
workbook.save(filename = "test.xlsx") # 保存表格
from openpyxl import load_workbook
workbook = load_workbook(filename = "test.xlsx") # 打开并获取表格
sheet = workbook.active # 获取当前活动的sheet
print(sheet)
sheet["D1"] = "标准身高"
for i in range(1,9):
sheet["D{}".format(i)] = '=IF(RIGHT(C{},2)="cm",C{},SUBSTITUTE(C{},"m","")*100&"cm")'.format(i,i,i) # 使用函数公式,实现单位转换:将单位为m的数值转换为单位为cm的数值
workbook.save(filename = "test.xlsx") # 保存文件
查看支持的excel函数公式:
from openpyxl.utils import FORMULAE
print(FORMULAE)
from openpyxl import load_workbook
workbook = load_workbook(filename = "test.xlsx") # 打开并获取表格
sheet = workbook.active # 获取当前活动的sheet
sheet.insert_cols(idx=4,amount=2) # 插入空列,即在第4列开始插入2列空列
sheet.insert_rows(idx=5,amount=4) # 插入空行,即在第5行开始插入4行空行
workbook.save(filename = "test.xlsx") # 保存文件
# .insert_cols(idx=数字编号, amount=要插入的列数),插入的位置是在idx列数的左侧插入;
# .insert_rows(idx=数字编号, amount=要插入的行数),插入的行数是在idx行数的下方插入;
from openpyxl import load_workbook
workbook = load_workbook(filename = "test.xlsx") # 打开并获取表格
sheet = workbook.active # 获取当前活动的sheet
# 删除第一列,第一行
sheet.delete_cols(idx=1)
sheet.delete_rows(idx=1)
workbook.save(filename = "test.xlsx") # 保存文件
from openpyxl import load_workbook
workbook = load_workbook(filename = "test.xlsx") # 打开并获取表格
sheet = workbook.active # 获取当前活动的sheet
# 向左移动两列,向下移动两行
sheet.move_range("B6:C7",rows=2,cols=2) # 移动单元格,即B6到C7的所有单元格,向右移动两个,向下移动两格
workbook.save(filename = "test.xlsx") # 保存文件
# .move_range(“数据区域”,rows=,cols=):正整数为向下或向右、负整数为向左或向上;rows表示行左右移动,cols表示列上下移动
from openpyxl.styles import Font
from openpyxl import load_workbook
workbook = load_workbook(filename="test.xlsx") # 打开并获取表格
sheet = workbook.active # 获取当前活动的sheet
cell = sheet["A1"] # 获取A1单元格内容
font = Font(name="微软雅黑",size=20,bold=True,italic=True,color="FF0000") # color是RGB的16进制表示
cell.font = font # 修改单元格样式
workbook.save(filename = "test.xlsx")
from openpyxl import load_workbook
workbook = load_workbook(filename="test.xlsx") # 打开并获取表格
sheet = workbook.active # 获取当前活动的sheet
cell = sheet["A1"] # 获取A1单元格内容
font = cell.font # 获取表格中格子的字体样式
print(font.name, font.size, font.bold, font.italic, font.color)
运行结果:
微软雅黑 20.0 True True <openpyxl.styles.colors.Color object>
Parameters:
rgb='00FF0000', indexed=None, auto=None, theme=None, tint=0.0, type='rgb'
Process finished with exit code 0
from openpyxl.styles import Alignment
from openpyxl import load_workbook
workbook = load_workbook(filename="test.xlsx") # 打开并获取表格
sheet = workbook.active # 获取当前活动的sheet
cell = sheet["A1"] # 获取A1单元格内容
alignment = Alignment(horizontal="center",vertical="center",text_rotation=45,wrap_text=True)
cell.alignment = alignment # 设置单元格对齐样式
workbook.save(filename = "test.xlsx")
# Alignment(horizontal=水平对齐模式,vertical=垂直对齐模式,text_rotation=旋转角度,wrap_text=是否自动换行)
# 水平对齐:‘distributed’,‘justify’,‘center’,‘leftfill’, ‘centerContinuous’,‘right,‘general’;
# 垂直对齐:‘bottom’,‘distributed’,‘justify’,‘center’,‘top’;
from openpyxl import load_workbook
from openpyxl.styles import Border, Side
workbook = load_workbook(filename="test.xlsx") # 打开并获取表格
sheet = workbook.active # 获取当前活动的sheet
cell = sheet["D6"]
side1 = Side(style="thin",color="FF0000")
side2 = Side(style="thick",color="FFFF0000")
border = Border(left=side1,right=side1,top=side2,bottom=side2)
cell.border = border # 设置该单元格的边框样式
workbook.save(filename = "test.xlsx")
查看边框线的内置 styles:
from openpyxl.styles import Border, Side, borders
print(dir(borders))
from openpyxl.styles import PatternFill,GradientFill
from openpyxl import load_workbook
workbook = load_workbook(filename="test.xlsx") # 打开并获取表格
sheet = workbook.active # 获取当前活动的sheet
cell_b9 = sheet["B9"]
pattern_fill = PatternFill(fill_type="solid",fgColor="99ccff")
cell_b9.fill = pattern_fill # 填充单元格
cell_b10 = sheet["B10"]
gradient_fill = GradientFill(stop=("FFFFFF","99ccff","000000"))
cell_b10.fill = gradient_fill # 填充单元格
workbook.save(filename = "test.xlsx")
# PatternFill(fill_type=填充样式,fgColor=填充颜色)
# GradientFill(stop=(渐变颜色1,渐变颜色2……))
查看支持的单元格填充样式:
from openpyxl.styles import PatternFill,GradientFill
print(gradient_fill)
from openpyxl import load_workbook
workbook = load_workbook(filename="test.xlsx") # 打开并获取表格
sheet = workbook.active # 获取当前活动的sheet
# 设置第1行的高度
sheet.row_dimensions[1].height = 50
# 设置B列的宽度
sheet.column_dimensions["B"].width = 20
workbook.save(filename = "test.xlsx")
"""
sheet.row_dimensions.height = 50
sheet.column_dimensions.width = 30
这两句代码,是将整个表的行高设置为50,列宽设置为30;
"""
from openpyxl import load_workbook
workbook = load_workbook(filename="test.xlsx") # 打开并获取表格
sheet = workbook.active # 获取当前活动的sheet
sheet.merge_cells("C1:D2") # 合并C1到D2的单元格
sheet.merge_cells(start_row=7,start_column=1,end_row=8,end_column=3)
workbook.save(filename = "test.xlsx")
# .merge_cells(待合并的格子编号)
# .merge_cells(start_row=起始行号,start_column=起始列号,end_row=结束行号,end_column=结束列号)
# 取消合并单元格:
# .unmerge_cells(待合并的格子编号)
# .unmerge_cells(start_row=起始行号,start_column=起始列号,end_row=结束行号,end_column=结束列号)
from openpyxl import load_workbook
workbook = load_workbook(filename = "test.xlsx") # 打开并获取表格
sheet = workbook.active # 获取当前活动的sheet
workbook.create_sheet("这是一个新的sheet")
print(workbook.sheetnames)
workbook.save(filename = "test.xlsx")
workbook.remove(sheet)
from openpyxl import load_workbook
workbook = load_workbook(filename = "a.xlsx") # 打开并获取表格
sheet = workbook.active # 获取当前活动的sheet
print("a.xlsx中有这几个sheet表:",workbook.sheetnames)
sheet = workbook['姓名'] # 获取名为“姓名”的工作表
workbook.copy_worksheet(sheet) # 复制该工作表
workbook.save(filename = "a.xlsx") # 保存文件
from openpyxl import load_workbook
workbook = load_workbook(filename = "a.xlsx") # 打开并获取表格
sheet = workbook.active # 获取当前活动的sheet
sheet.title = "这是修改后的sheet名" # 修改工作表名
print(sheet)
workbook.save(filename = "a.xlsx") # 保存文件
from openpyxl import Workbook
workbook = Workbook() # 获取一个表格
sheet = workbook.active # 获取当前活动的sheet
sheet.title = "表格1" # 修改工作表名
workbook.save(filename = "新建的excel表格.xlsx") # 保存文件
from openpyxl import load_workbook
workbook = load_workbook(filename = "test.xlsx") # 打开并获取表格
sheet = workbook.active # 获取当前活动的sheet
sheet.freeze_panes = ("A2") # 冻结首行
workbook.save(filename = "test.xlsx") # 保存文件
与 Excel 相似,唯一的不同是,Excel 中是写入单元格,而在 Word 中则是在文档的文本“范围”内插入字符串。
from tkinter import Tk
from time import sleep
from tkinter.messagebox import showwarning
import win32com.client as win32
warn=lambda app:showwarning(app,'即将退出进程')
Range=range(3,8)
def word():
app='Word'
word=win32.gencache.EnsureDispatch('%s.Application'%app) # 调度 word 进程
doc=word.Documents.Add() # 添加一个 word 文档
word.Visible=True # 显示该文档
sleep(1)
rng=doc.Range(0,0)
rng.InsertAfter('Python-to-%s Test \r\n\r\n'%app)
sleep(1)
for i in Range:
rng.InsertAfter('line %d\r\n'%i)
sleep(1)
rng.InsertAfter("\r\n th-th-th-that")
warn(app) # 弹出警告框
doc.Close(False) # 不保存并关闭
word.Application.Quit() # 退出进程
if __name__=='__main__':
Tk().withdraw()
word()