python pywin32模块操作office,基本类.

word_base.py

 

 

import os
from win32com.client import DispatchEx
import  config

class WordWrap:

    def __init__(self, templatefile=None):
        self.filename = templatefile
        self.wordApp = DispatchEx('Word.Application')
        if templatefile == None:
            self.wordDoc = self.wordApp.Documents.Add()
        else:
            self.wordDoc = self.wordApp.Documents.open(templatefile)
        #set up the selection
        self.wordDoc.Range(0,0) .Select()
        self.wordSel = self.wordApp.Selection
        # #fetch the styles in the document - see below
        # self.wordDoc.getStyleDictionary()
    def show(self):
        #自动化操作的界面显示出来
        self.wordApp.Visible = 1
    def saveAs(self, filename):
        #另存为
        self.wordDoc.SaveAs(filename)
    def printout(self):
        #打印出来
        self.wordDoc.PrintOut()
    def selectEnd(self):
        # 选中整个文档末尾
        self.wordSel.Collapse(0)

    def addText(self, text):
        self.wordSel.InsertAfter(text)
        self.selectEnd()
    def save(self):
        # self.wordDoc.SaveAs(self.filename,12,False,True,False,False,False,False,False,0,False,False,0,False)
        self.wordDoc.Save()

    def close(self):
        self.wordDoc.Close(0)
        self.wordApp.Quit()
    def replace_word(self,old_str,new_str):
        self.wordSel.Find.ClearFormatting()
        self.wordSel.Find.Replacement.ClearFormatting()
        self.wordSel.Find.Execute(old_str,False,False,False,False,False,True,1,True,new_str,2)


    def replace_text(self,old_str,new_str):
        #替换正文所有匹配内容.
        self.wordApp.ActiveWindow.ActivePane.View.SeekView = 0
        self.replace_word(old_str,new_str)
    def replace_headers_text(self,old_str,new_str):
        #替换页眉所有匹配内容
        self.wordApp.ActiveWindow.ActivePane.View.SeekView = 1
        self.wordApp.ActiveDocument.Sections[0].Headers[0].Range.Find.ClearFormatting()
        self.wordApp.ActiveDocument.Sections[0].Headers[0].Range.Find.Replacement.ClearFormatting()
        self.wordApp.ActiveDocument.Sections[0].Headers[0].Range.Find.Execute(old_str, False, False, False, False, False, True, 1, False, new_str, 2)
        self.wordApp.ActiveWindow.ActivePane.View.SeekView = 0

    def replace_footers_text(self,old_str,new_str):
        #替换页眉所有匹配内容
        self.wordApp.ActiveWindow.ActivePane.View.SeekView = 10
        self.wordApp.ActiveDocument.Sections[0].Footers[0].Range.Find.ClearFormatting()
        self.wordApp.ActiveDocument.Sections[0].Footers[0].Range.Find.Replacement.ClearFormatting()
        self.wordApp.ActiveDocument.Sections[0].Footers[0].Range.Find.Execute(old_str, False, False, False, False, False, True, 1, False, new_str, 2)
        self.wordApp.ActiveWindow.ActivePane.View.SeekView = 0

    def update_catalog(self):
        '''更新目录'''
        self.wordApp.ActiveWindow.ActivePane.View.SeekView = 0
        self.wordApp.ActiveDocument.Fields(1).Update()

    def all_copy(self):
        '''选中全文并进行拷贝'''

        self.wordSel.WholeStory()
        self.wordSel.Copy()

    def paste_end(self):
        '''在word末尾粘贴剪贴板的内容'''
        self.wordSel.WholeStory()
        self.wordSel.setRange(self.wordSel.end,self.wordSel.end)
        self.wordSel.PasteAndFormat(16)

    def all_cut(self):
        '''选中全文并剪切'''
        self.wordSel.WholeStory()
        self.wordSel.Cut()

    def word_before(self,search_word):

        '''找到匹配单词的前面'''
        self.wordSel.SetRange(0,0)
        self.wordSel.Find.Wrap = 0
        self.wordSel.Find.Text=search_word
        self.wordSel.Find.MatchCase = False
        self.wordSel.Find.MatchByte = True
        self.wordSel.Find.MatchWildcards = False
        self.wordSel.Find.MatchWholeWord = False
        self.wordSel.Find.MatchFuzzy = False
        self.wordSel.Find.Replacement.Text = ''
        self.wordSel.Find.Execute()
        self.wordSel.SetRange(self.wordSel.start,self.wordSel.start)

    def paste_origin(self):
        '''根据原有格式粘贴,不改变格式'''
        self.wordSel.PasteAndFormat(16)



def test_demo():
    '''测试案列'''
    w_app = WordWrap(r'C:\Users\Administrator\Desktop\2015.docx')

    w_app.replace_word('质量','zhiliang')
    w_app.replace_headers_text('质量','zhiliang')
    w_app.replace_footers_text('质量','zhiliang')
    w_app.replace_footers_text('27','31')
    w_app.update_catalog()
    w_app.saveAs(r'G:\development\myproject\stu_win32\hahahaha')
    w_app.close()

def handler_file(file_path):
    '''对一个文件进行处理和保存'''
    w_app = WordWrap(file_path)

    for text_word in config.text_list:
        w_app.replace_word(text_word[0],text_word[1])
    for page_header in config.page_header_list:
        w_app.replace_headers_text(page_header[0],page_header[1])
    for page_footer in config.page_footer_list:
        w_app.replace_footers_text(page_footer[0],page_footer[1])

    w_app.update_catalog()

    w_app.saveAs(file_path)
    w_app.close()

def handle_dir_below(dir):
    '''处理一个目录下的所有word'''
    if not os.path.exists(dir):
        print('不存在该目录')
        return

    for root_dir,dir_list,file_list in os.walk(dir):
        for filename in file_list:
            if os.path.splitext(filename)[1] in ['.docx','.doc','.wps','.dot','.dotx','.dotm','.docm']:
                handler_file(os.path.join(root_dir,filename))
                new_file_name=filename.replace(config.file_name[0],config.file_name[1])
                os.rename(os.path.join(root_dir,filename),os.path.join(root_dir,new_file_name))


def main():
    for handler_dir in config.dir_list:
        handle_dir_below(handler_dir)

if __name__ == '__main__':
    main()

### 回答1: Python pywin32模块是一个用于Windows操作系统的Python扩展模块,它提供了访问Windows API的接口,可以让Python程序与Windows系统进行交互。该模块包含了许多子模块,如win32api、win32gui、win32com等,可以用于实现各种Windows应用程序的开发,如GUI应用程序、系统管理工具、自动化脚本等。在使用该模块时,需要先安装pywin32模块,并且需要了解Windows API的使用方法。 ### 回答2: Python是一种高级的解释性编程语言,它具有简单易用的特点,被广泛应用于众多领域,例如:科学计算、网络编程、Web开发等。 pywin32Python下的一个第三方库,它提供了许多Windows操作系统API的接口,封装了如PYMFC、COM、ActiveX等Windows API的一些函数和型,使Python程序员可以调用Windows系统级别API进行开发。 pywin32模块可以在Python下开发Windows应用程序的各个方面进行调用,包括文件操作、进程管理、网络编程、COM编程、窗口管理、事件处理等等。 在Windows环境下,使用pywin32库进行开发可以利用Windows的强大功能,比如通过调用Windows API来操作Win32 SDK下的极具特色的界面系统,或者利用COM规范来操作Windows下的各应用程序与服务。 要使用pywin32模块进行开发,需要遵循以下步骤: 1. 安装Python环境:需要安装Python解释器,并设置好相应的环境变量。 2. 安装pywin32模块:可以在pywin32的官方网站下载最新版本的安装包,然后进行安装。安装完后在Python环境下引入即可。 3. 导入pywin32模块:需要在Python代码中使用import pywin32进行模块导入。 4. 使用pywin32模块pywin32模块提供了许多API接口,开发者可根据需要进行调用。 pywin32模块的使用需要具备一定的Windows API编程经验,因为其中涉及到了操作系统的许多底层机制,对Python编程能力要求也比较高。 总体来说,pywin32模块Python编程的一个重要工具,在Windows下进行程序开发的时候,可以帮助开发者快速、高效地实现所需功能。 ### 回答3: Python pywin32模块Python语言的一种扩展工具,它用于在Microsoft Windows操作系统上访问Windows API函数库中的功能。它允许程序员使用Python语法和编程模型,来快速地利用Windows API的功能。 pywin32模块包含多个子模块,包括了Win32api、Win32con、Win32com、win32gui等。其中,Win32api子模块提供了与Windows API相关的函数;Win32con子模块定义了Windows API常量和消息;Win32com子模块实现了COM的Python控制器,可以访问COM接口;win32gui子模块提供了Python编程环境中的图形用户界面。所有这些模块都是为Windows API的功能提供Python接口的。 Pywin32模块的优点包括: (1) 在Windows操作系统中使用Python语言,可以更容易地访问Windows API的功能; (2) 支持使用COM接口,可以通过Python来访问Windows内置的许多功能,比如Microsoft Office应用程序; (3) 可以创建和控制Windows窗体,可以使Python语言的图形界面更加生动和强大。 总之,Python pywin32模块Python程序员提供了一种访问Windows API函数库的途径,其使用非常灵活和方便,非常适合在Windows上进行Python编程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值