吴裕雄 实战PYTHON编程(7)

import os

from win32com import client

word = client.gencache.EnsureDispatch(‘Word.Application‘)
word.Visible = 1
word.DisplayAlerts = 0
doc = word.Documents.Add()
range1 = doc.Range(0,0) #文件起始处
range1.InsertAfter("这是测试第一行\n这是测试第二行\n")
range1.InsertAfter("这是测试第三行\n这是测试第四行\n")
range1.InsertBefore("第一次插入到文件最前方\n")
range1.InsertBefore("再次插入到文件最前方\n")
# cpath = os.path.dirname(__file__)
doc.SaveAs("F:\\pythonBase\\pythonex\\test1.docx")
doc.Close()
word.Quit()

import os

from win32com import client

word = client.gencache.EnsureDispatch(‘Word.Application‘)
word.Visible = 0
word.DisplayAlerts = 0
# cpath=os.path.dirname(__file__)
doc = word.Documents.Open("F:\\pythonBase\\pythonex\\test1.docx")
print(doc.Content)
doc.Close()
word.Quit()

import os

from win32com import client

word = client.gencache.EnsureDispatch(‘Word.Application‘)
word.Visible = 0
word.DisplayAlerts = 0
doc = word.Documents.Open("F:\\pythonBase\\pythonex\\test1.docx")
paragraphs = doc.Paragraphs
for p in paragraphs:
text = p.Range.Text.strip()
print(text)
doc.Close()
word.Quit()

import os

from win32com import client

word = client.gencache.EnsureDispatch(‘Word.Application‘)
word.Visible = 0
word.DisplayAlerts = 0
doc = word.Documents.Open("F:\\pythonBase\\pythonex\\test1.docx")
paragraphs = doc.Paragraphs
print("第一段:" + paragraphs(1).Range.Text.strip())
print("第三段:" + paragraphs(3).Range.Text.strip())
doc.Close()
word.Quit()

import os
import time

from win32com import client
from win32com.client import constants

word = client.gencache.EnsureDispatch(‘Word.Application‘)
word.Visible = 1
word.DisplayAlerts = 0
doc = word.Documents.Open("F:\\pythonBase\\pythonex\\test1.docx")
paragraphs = doc.Paragraphs
range1 = paragraphs(1).Range #第1段
range1.Style = constants.wdStyleHeading1
range1.Style.Font.Name = "标楷体"
range1.Style.Font.Color = 0xFF0000 #蓝色
range1.Style.Font.Bold = 1 #Italic, Underline, Shadow, Outline

range2 = paragraphs(2).Range #第2段
range2.Style = constants.wdStyleHeading3
range2.ParagraphFormat.Alignment = constants.wdAlignParagraphRight

range3 = paragraphs(3).Range #第3段
range3.Style.Font.Size = "10"
doc.Close()
word.Quit()
time.sleep(3)

import os
from win32com import client

word = client.gencache.EnsureDispatch(‘Word.Application‘)
word.Visible = 1
word.DisplayAlerts = 0
doc = word.Documents.Open("F:\\pythonBase\\pythonex\\08\\clipgraph.docx")
data = [ ["型号", "尺寸", "颜色", "价格"], ["A8", "5.0英寸", "白色", "8000"],["A10", "5.5 英寸", "金黄", "22000"] ]
paragraphs = doc.Paragraphs #读取所有段落
range1 = paragraphs(4).Range #取第4段的起止范围
table = doc.Tables.Add(range1, 3, 4) #在第4段之前插入一个3行4列表格
for i in range(1,table.Rows.Count+1): #i取值分别为1~3。切记rang()函数的特性
print(i)
# for j in range(1,table.Columns.Count+1): #j取值分别为1~4
# table.Cell(i,j).Range.Text = data[i-1][j-1] #第一次会把data[0][0],即"型号",插入Cell(1,1)
#table.Cell(2,3).Range.Font.Color = 0x0000FF #设置第2行第3列单元格的字体颜色
#doc.Close()
#word.Quit()

import os
from win32com import client

word = client.gencache.EnsureDispatch(‘Word.Application‘)
word.Visible = 1
word.DisplayAlerts = 0
doc = word.Documents.Open("F:\\pythonBase\\pythonex\\clipgraph.docx")
paragraphs = doc.Paragraphs
range1 = paragraphs(4).Range
range1.InlineShapes.AddPicture(cpath + "F:\\pythonBase\\pythonex\\ch08\\media\\cell.jpg", False, True)
#doc.Close()
#word.Quit()

import os
from win32com import client as client
from win32com.client import constants
word = client.gencache.EnsureDispatch(‘Word.Application‘)
word.Visible = 1
word.DisplayAlerts = 0
cpath=os.path.dirname(__file__)
doc = word.Documents.Open(cpath + "\\media\\clipgraph.docx")
word.Selection.Find.ClearFormatting()
word.Selection.Find.Replacement.ClearFormatting()
word.Selection.Find.Execute("方法",False,False,False,False,False,True,constants.wdFindContinue,False,"method",constants.wdReplaceAll)
#doc.Close()
#word.Quit()

def getrandom2(n1, n2): #取得2个不重复的随机值
while True:
r1 = random.randint(n1, n2)
r2 = random.randint(n1, n2)
if r1 != r2: #如果两数不相等就跳出,相等则继续取
break
return r1, r2

import os, random
from win32com import client
from win32com.client import constants
word = client.gencache.EnsureDispatch(‘Word.Application‘)
word.Visible = 1
word.DisplayAlerts = 0 #不显示警告
doc = word.Documents.Add()
range1 = doc.Range(0,0) #文件开头
range1.Style.Font.Size = "16" #字体大小
title = "明星小学营养午餐菜单"
year1 = "2017年8月"
week = ["一","二","三","四","五"]
teacher = ["欧阳怡","翟定国","陈碧山","陈丽娟","郑怡衡","林邓超","朱健政","刘伟明","刘维基","梁银燕"]
rice = ["糙米饭","白米饭","拌面"]
vegetable = ["毛豆白菜","豆芽菜","蛋香时瓜","高丽菜","佛手瓜","酸菜豆包","冬瓜","萝卜海带结","茄汁洋芋","家常豆腐","鲜菇花椰","豆皮三丝","伍彩雪莲","干香根丝","茄汁豆腐","香炒花椰","芹香粉丝","红萝卜","洋葱","青椒"]
meat = ["糖醋排骨","美味大鸡腿","椒盐鱼条","香菇肉燥","宫保鸡丁","香卤腿排","梅干绞肉","香酥鱼丁","条瓜烧鸡","时瓜肉丝","海结卤肉","葱烧鸡","柳叶鱼","咖哩绞肉","笋香鸡","沙茶猪柳","五香棒腿","三杯鸡丁","海结猪柳","茄汁鸡丁"]
soup = ["蛋香木须汤","味噌海芽汤","绿豆汤","榨菜肉丝汤","姜丝海芽汤","枸杞爱玉汤","冬菜蛋花汤","冬瓜西米露","紫菜蛋花汤","蛋香木须汤"]
date1= 1 #开始日期为1日
weekday = 2 #开始日期为星期六

while weekday < 6 and date1 < 31: #周一到周五及30日前才制作菜单
range1.InsertAfter(title + "\n")
range1.InsertAfter("日期:" + year1 + str(date1) + "日 (星期" + week[weekday-1] + ")\n")
range1.InsertAfter("制作者:" + teacher[random.randint(0,9)] + "老师\n") #10位老师中随机选一位
range1.InsertAfter("今日菜单:\n")
range1.InsertAfter("一、" + rice[random.randint(0,2)] + "\n") #取1个随机数作为主食列表下标
rand1, rand2 = getrandom2(0,19) #取得两个随机数,作为菜品列表下标
range1.InsertAfter("二、" + vegetable[rand1] + "\n")
range1.InsertAfter("三、" + vegetable[rand2] + "\n")
rand1, rand2 = getrandom2(0,19) #重取两个随机数,作为肉品列表下标
range1.InsertAfter("四、" + meat[rand1] + "\n")
range1.InsertAfter("五、" + meat[rand2] + "\n")
range1.InsertAfter("六、" + soup[random.randint(0,9)] + "\n") #取一个随机数,作为汤品列表下标
range1.Collapse(constants.wdCollapseEnd) #移到range尾
range1.InsertBreak(constants.wdSectionBreakNextPage) #换页
weekday += 1 #星期加1
date1 += 1 #日期加1
if weekday == 6: #如果是星期六
weekday = 1 #设为星期一
date1 += 2 #日期加2(星期六及星期日)

cpath=os.path.dirname(__file__)
doc.SaveAs(cpath + "\\media\\food.docx") #获取文件保存路径并把文件保存为food.docx
#doc.Close()
#word.Quit()

import os
from win32com import client
from win32com.client import constants
word = client.gencache.EnsureDispatch(‘Word.Application‘)
word.Visible = 0
word.DisplayAlerts = 0
runpath = os.path.dirname(__file__) + "\\replace" #获取replace文件夹的路径
tree = os.walk(runpath) #取得目录树
print("所有 Word 文件:")
for dirname, subdir, files in tree:
allfiles = []
for file in files: # 取得所有.docx .doc文件,存入allfiles列表中
ext = file.split(".")[-1] #取得文件名后缀
if ext=="docx" or ext=="doc": #取得所有.docx .doc文件
allfiles.append(dirname + ‘\\‘ + file) #加入allfiles列表

if len(allfiles) > 0: #如果有符合条件的文件
for dfile in allfiles:
print(dfile)
doc = word.Documents.Open(dfile) #打开文件
word.Selection.Find.ClearFormatting()
word.Selection.Find.Replacement.ClearFormatting()
word.Selection.Find.Execute("方法",False,False,False,False,False,True,constants.wdFindContinue,False,"method",constants.wdReplaceAll)
doc.Close()
word.Quit()

原文地址:https://www.cnblogs.com/tszr/p/10061326.html

时间: 2024-08-28 04:46:24

吴裕雄 实战PYTHON编程(7)的相关文章

吴裕雄 实战python编程(3)

import requests from bs4 import BeautifulSoup url = 'http://www.baidu.com'html = requests.get(url)sp = BeautifulSoup(html.text, 'html.parser')print(sp) html_doc = """<html><head><title>页标题</title></head> <p

吴裕雄 实战PYTHON编程(8)

import pandas as pd df = pd.DataFrame( {"林大明":[65,92,78,83,70], "陈聪明":[90,72,76,93,56], "黄美丽":[81,85,91,89,77], "熊小娟":[79,53,47,94,80] } )print(df) import pandas as pd datas = [[65,92,78,83,70], [90,72,76,93,56], [8

吴裕雄 实战PYTHON编程(6)

import matplotlib.pyplot as plt plt.rcParams['font.sans-serif']=['Simhei']plt.rcParams['axes.unicode_minus']=False listx1 = [1,5,7,9,13,16]listy1 = [15,50,80,40,70,50]plt.bar(listx1, listy1, label="男性")listx2 = [2,6,8,11,14,16]listy2 = [10,40,30

吴裕雄 实战PYTHON编程(9)

import cv2 cv2.namedWindow("ShowImage1")cv2.namedWindow("ShowImage2")image1 = cv2.imread("F:\\pythonBase\pythonex\\ch10\\media\\img01.jpg")#image1 = cv2.imread("media\\img01.jpg", 1)image2 = cv2.imread("F:\\pyt

Python编程实战:运用设计模式、并发和程序库创建高质量程序 阅读笔记

Python编程实战:运用设计模式.并发和程序库创建高质量程序 目录 1 创建型设计模式 2 结构型设计模式 3 行为型设计模式 4 高级并发 5 扩充Python 6 高级网络编程 7 Tkinter 8 OpenGL 创建型设计模式 抽象工厂 @classmethod def make_xxx(Class, ...) Builder with open(filename, "w", encoding='utf-8') as f: f.write(x) 多一层映射封装好吗? 序列与m

树莓派Python编程入门与实战 PDF

树莓派Python编程入门与实战 链接:https://pan.baidu.com/s/1y2-912g7VknVzXKAdzUPpA 密码:rzbz 原文地址:https://www.cnblogs.com/luoshuifusheng/p/9376025.html

从能做什么到如何去做,一文带你快速掌握Python编程基础与实战

摘要:Python语言的教程虽然随处可见,但是忙于日常业务/学习的你或许:一直想要"找个时间学一点",但是又不知道该从何下手?本文将从Python能做什么,如何学习Python以及Python的基础知识为你的Python之路点上一盏明灯. 本文内容根据演讲视频以及PPT整理而成. 本文的分享主要围绕以下几个方面: Python能做什么?(常见应用场景介绍) 如何学习Python? Python语法基础实战 Python面向对象编程实战 练熟基础:2048小游戏项目的实现与实战 福利:小

python经典书籍:Python编程实战 运用设计模式、并发和程序库创建高质量程序

Python编程实战主要关注了四个方面 即:优雅编码设计模式.通过并发和编译后的Python(Cython)使处理速度更快.高层联网和图像.书中展示了在Python中已经过验证有用的设计模式,用专家级的代码阐释了这些设计模式,并解释了为什么一些与面向对象设计相关的模式和Python均有关联. 书中通过大量实用的范例代码和三个完整的案例研究,全面而系统地讲解 了如何运用设计模式来规划代码结构,如何通过 并发与Cython等技术提升代码执行速度,以及如 何利用各科IPython程序库来快速开发具体的

Python编程(二):Python进程、线程那点事儿

多进程,多线程编程 系统程序员.运维开发程序员在面试的时候经常会被问及一个常见问题: 进程是什么,线程是什么,进程和线程有什么区别? 不得不承认,这么多年了.这个问题依旧是个很难以招架的问题,简单地说: 进程和线程有很多类似的性质,他们都可以被CPU作为一个单元进行调度,他们都拥有自己独立的栈(stack)等等.因此线程也被称作LWP(Lightweight Process,轻量级进程):对应的进程也可以被称作为HWP(Heavyweight Process,重量级进程),从线程的角度看,进程就