python应用系列教程——python操作office办公软件word和excel!

python应用系列教程——python操作office办公软件word和excel!

python操作office办公软件(excel)。

前提条件是电脑已经安装了office办公软件,并且下载安装了pywin32-217.win32-py2.7库。

python2.7下代码

#coding:utf-8
#python控制excel软件,本机电脑需要安装office软件
from Tkinter import Tk
from time import sleep
from tkMessageBox import showwarning
import win32com.client as win32
warn = lambda app: showwarning(app, 'Exit?') #弹出提示框
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 = 'first line'
 sleep(1) #暂停一下,让用户看清演示的每一步
 for i in range(3, 8):
 sh.Cells(i,1).Value = 'line %d' % i #在3到8行,第一列,写入内容
 sleep(1) #暂停一下,让用户看清演示的每一步
 sh.Cells(i+2,1).Value = "last line"
 sh.Range(sh.Cells(1, 1), sh.Cells(4, 1)).Font.Bold = True #设置指定区域的字体格式
 warn(app) #弹出警告消息
 ss.Close(False) #工作簿关闭保存
 xl.Application.Quit() #excel应用退出
if __name__=='__main__':
 Tk().withdraw() #不让tk顶级窗口出现,因为默认tk会自动创建一个顶级窗口,而且不会将其隐藏
 excel()

python3.6下代码

python应用系列教程——python操作office办公软件word和excel!

#coding:utf-8
#python控制excel软件,本机电脑需要安装office软件
from tkinter import Tk
from time import sleep
from tkinter.messagebox import showwarning
import win32com.client as win32
warn = lambda app: showwarning(app, 'Exit?') #弹出提示框
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 = 'first line'
 sleep(1) #暂停一下,让用户看清演示的每一步
 for i in range(3, 8):
 sh.Cells(i,1).Value = 'line %d' % i #在3到8行,第一列,写入内容
 sleep(1) #暂停一下,让用户看清演示的每一步
 sh.Cells(i+2,1).Value = "last line"
 sh.Range(sh.Cells(1, 1), sh.Cells(4, 1)).Font.Bold = True #设置指定区域的字体格式
 warn(app) #弹出警告消息
 ss.Close(False) #工作簿关闭保存
 xl.Application.Quit() #excel应用退出
if __name__=='__main__':
 Tk().withdraw() #不让tk顶级窗口出现,因为默认tk会自动创建一个顶级窗口,而且不会将其隐藏
 excel()

python操作office办公软件(doc)。

python应用系列教程——python操作office办公软件word和excel!

前提条件是电脑已经安装了office办公软件,并且下载安装了pywin32-217.win32-py2.7库。

然后就可以使用python编程操作word软件了。word软件的启动有时较慢,所以可能需要等待几秒中才能启动成功。

python2.7下代码如下

#coding:utf-8
#python操作word
from Tkinter import Tk
from time import sleep
from tkMessageBox import showwarning
import win32com.client as win32
warn = lambda app: showwarning(app, 'Exit?') #弹出提示框
def word():
 app = 'Word'
 word = win32.gencache.EnsureDispatch('%s.Application' % app) #创建word对象 
 doc = word.Documents.Add() #添加一个文档
 word.Visible = True #设置为桌面显示可见
 sleep(1) #暂停一下,让用户看清演示的每一步
 rng = doc.Range(0,0) #定位光标位置
 rng.InsertAfter('first line

') #在光标后加入内容
 sleep(1) #暂停一下,让用户看清演示的每一步
 for i in range(3, 8):
 rng.InsertAfter('Line %d
' % i) #在光标后加入内容
 sleep(1) #暂停一下,让用户看清演示的每一步
 rng.InsertAfter("
last line
") #在光标后加入内容
 warn(app) #弹出警告消息
# doc.Save("test.doc") #保存
 doc.Close(False) #文档关闭
 word.Application.Quit() #word应用退出
if __name__=='__main__':
 Tk().withdraw() #不让tk顶级窗口出现,因为默认tk会自动创建一个顶级窗口,而且不会将其隐藏
 word()

python3.6中代码如下

#coding:utf-8
#python操作word
from tkinter import Tk
from time import sleep
from tkinter.messagebox import showwarning
import win32com.client as win32
warn = lambda app: showwarning(app, 'Exit?') #弹出提示框
def word():
 app = 'Word'
 word = win32.gencache.EnsureDispatch('%s.Application' % app) #创建word对象 
 doc = word.Documents.Add() #添加一个文档
 word.Visible = True #设置为桌面显示可见
 sleep(1) #暂停一下,让用户看清演示的每一步
 rng = doc.Range(0,0) #定位光标位置
 rng.InsertAfter('first line

') #在光标后加入内容
 sleep(1) #暂停一下,让用户看清演示的每一步
 for i in range(3, 8):
 rng.InsertAfter('Line %d
' % i) #在光标后加入内容
 sleep(1) #暂停一下,让用户看清演示的每一步
 rng.InsertAfter("
last line
") #在光标后加入内容
 warn(app) #弹出警告消息
# doc.Save("test.doc") #保存
 doc.Close(False) #文档关闭
 word.Application.Quit() #word应用退出
if __name__=='__main__':
 Tk().withdraw() #不让tk顶级窗口出现,因为默认tk会自动创建一个顶级窗口,而且不会将其隐藏
 word()

猜你喜欢

转载自blog.csdn.net/qq_41841569/article/details/82984703