加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python操作office办公软件word

发布时间:2020-12-20 10:17:57 所属栏目:Python 来源:网络整理
导读:直接上python3.6中代码 [Python]? 纯文本查看? 复制代码 ? 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #coding:utf-8 # python操作word from tkinter import Tk from time import sleep from tkinte

直接上python3.6中代码

[Python]? 纯文本查看?复制代码
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#coding:utf-8
# python操作word
from tkinter import Tk
from time import sleep
from tkinter.messagebox import showwarning
import win32com.client as win
?
warn = lambda app: showwarning(app, ‘Exit?‘ )?? # 弹出提示框
?
def word():
???? app = ‘Word‘
???? word = win.gencache.EnsureDispatch( ‘%s.Application‘ % app)?? # 创建word对象
???? doc = word.Documents.Add() #添加一个文档
???? word.Visible = True? #设置为桌面显示可见
???? sleep( 1 )? #暂停一秒,让用户看清演示的每一步
?
???? rng = doc. Range ( 0 , 0 )?? # 定位光标位置
???? rng.InsertAfter( ‘first linernrn‘ )?? # 在光标后加入内容
???? sleep( 1 )?
???? for i in range ( 3 , 10 ):
???????? rng.InsertAfter( ‘Line %drn‘ % i)?? # 在光标后加入内容
???????? sleep( 1 )?
???? rng.InsertAfter( "rnlast linern" )?? # 在光标后加入内容
?
???? warn(app)??? # 弹出警告消息
#???? doc.Save("test.doc")?? # 保存
???? doc.Close( False )?? # 关闭
???? word.Application.Quit()?? # word应用退出
?
if __name__ = = ‘__main__‘ :
???? Tk().withdraw()?? # 不让tk顶级窗口出现,因为默认tk会自动创建一个顶级窗口,而且不会将其隐藏
???? word()

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读