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

word文档批量修改

发布时间:2020-12-17 17:20:41 所属栏目:Python 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 #!/usr/bin/python"""-----------------------------------------------------------------------------------2015.07.16 Grey**********************

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

#!/usr/bin/python
"""
-----------------------------------------------------------------------------------
2015.07.16   Grey
***********************************************************************************
Use the win32com to process word document and generate a set of ENW documents.
"""
import win32com,os,re,time,datetime,string
from win32com.client import Dispatch,constants

def enw_documents_generate(document_name):
	enw_document_type                   = raw_input("Please input the type for ENW Doc (E/e for electronic and others for paper):").upper()
	pwd                                 = os.getcwd()
	document_list                       = []
	hex_list                            = []
	pack_flag_for_replace               = 'pack_name'
	word_app                            = win32com.client.Dispatch('Word.Application')
	word_app.Visible                    = 0
	word_app.DisplayAlerts              = 0
	document_name                       = pwd + '' + document_name
	base_time                           = time.strftime('%Y_%m_%d',time.localtime(time.time()))
	date_today                          = datetime.datetime(string.atoi(base_time[0:4]),string.atoi(base_time[5:7]),string.atoi(base_time[8:10]))
	date_three_days_later               = date_today + datetime.timedelta(days = 85)
	date_today_str                      = re.sub(r' 00.00.00','',str(date_today))
	date_three_days_later_str           = re.sub(r' 00.00.00',str(date_three_days_later))
	date_today_str                      = re.sub(r'(dddd)-(dd)-(dd)',r'2/3/1',date_today_str)
	date_three_days_later_str           = re.sub(r'(dddd)-(dd)-(dd)',date_three_days_later_str)
	#document                            = word_app.Documents.Open(FileName = document_name)
	#word_app.Selection.Find.ClearFormatting()  
	#word_app.Selection.Find.Replacement.ClearFormatting()
	for root,dirs,files in os.walk('.'):
		hex_list               = [f for f in os.listdir('.') if f.endswith('.rar')]
	for hex_file in hex_list:
		pack_name = re.sub('.rar',hex_file)
		document                            = word_app.Documents.Open(FileName = document_name)
		word_app.Selection.Find.ClearFormatting()  
		word_app.Selection.Find.Replacement.ClearFormatting()
		word_app.Selection.Find.Execute('pack_name',False,True,1,pack_name,2)
		word_app.Selection.Find.Execute('today',date_today_str,2)
		if enw_document_type == 'E':
			word_app.Selection.Find.Execute('Three_days_later',2)
		else :
			word_app.Selection.Find.Execute('Three_days_later',date_three_days_later_str,2)
		document.SaveAs(pwd + '' + 'CTENW_' + pack_name +'.docx')
	#document.Close()
	word_app.Documents.Close() 
	word_app.Quit() 

enw_documents_generate('ENW_temp.docx')
print "All the ENW documents has been generated!"

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

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

    推荐文章
      热点阅读