資源簡介
操作word,遍歷磁盤,為所選磁盤下所有doc和docx文件插入文本框
代碼片段和文件信息
‘‘‘
Created?on?Sep?19?2014
@author:?liu.chunming
‘‘‘
#!/usr/bin/env?python??
#coding=utf-8??
?
import?os
from?win32com.client?import?Dispatch
import?string
class?ScanFile(object):?
????def?__init__(selfdirectory):
????????self.directory=directory
????????
????def?scan_files(self):??
????????files_list=[]??????
????????for?dirpathdirnamesfilenames?in?os.walk(self.directory):?
????????????for?special_file?in?filenames:??
????????????????if?special_file.endswith(‘.doc‘)?or?special_file.endswith(‘.docx‘):
????????????????????if?not?special_file.startswith(‘~‘):
????????????????????????files_list.append(os.path.join(dirpathspecial_file))???????????????????????????
????????return?files_list
def?addwatermark(dir):
????word?=?Dispatch(‘Word.Application‘)?????#?打開word應用程序
????word.Visible?=?0????????#?后臺運行不顯示
????word.DisplayAlerts?=?0??#?不警告
????print(“正在處理:“+file)
????try:
????????doc?=?word.Documents.Open(FileName=file?Encoding=‘gbk‘)
????except?:
????????print(“發生錯誤:“+file)
- 上一篇:python實現 九九乘法口訣表
- 下一篇:python 將視頻轉為 字符畫
評論
共有 條評論