免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1154 | 回复: 0
打印 上一主题 下一主题

word文档批量修改 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-07-22 16:51 |只看该作者 |倒序浏览
工作中需要一次次改word文档,但是格式又非常死板。于是想做成一个自动化的生成方式。
网络上找了一点代码拼凑修改了一下,虽然有点小异常,但是还是能够完成我要的功能了。改一部分文字啊什么的还是很OK啦!
具体的用法也没有仔细研究,先凑合用。
auto_doc_generate.py
  1. #!/usr/bin/python
  2. """
  3. -----------------------------------------------------------------------------------
  4. 2015.07.16   Grey
  5. ***********************************************************************************
  6. Use the win32com to process word document and generate a set of ENW documents.
  7. """
  8. import win32com,os,re,time,datetime,string
  9. from win32com.client import Dispatch,constants

  10. def enw_documents_generate(document_name):
  11.     enw_document_type                   = raw_input("Please input the type for ENW Doc (E/e for electronic and others for paper):").upper()
  12.     pwd                                 = os.getcwd()
  13.     document_list                       = []
  14.     hex_list                            = []
  15.     pack_flag_for_replace               = 'pack_name'
  16.     word_app                            = win32com.client.Dispatch('Word.Application')
  17.     word_app.Visible                    = 0
  18.     word_app.DisplayAlerts              = 0
  19.     document_name                       = pwd + '\\' + document_name
  20.     base_time                           = time.strftime('%Y_%m_%d',time.localtime(time.time()))
  21.     date_today                          = datetime.datetime(string.atoi(base_time[0:4]),string.atoi(base_time[5:7]),string.atoi(base_time[8:10]))
  22.     date_three_days_later               = date_today + datetime.timedelta(days = 85)
  23.     date_today_str                      = re.sub(r' 00.00.00','',str(date_today))
  24.     date_three_days_later_str           = re.sub(r' 00.00.00','',str(date_three_days_later))
  25.     date_today_str                      = re.sub(r'(\d\d\d\d)-(\d\d)-(\d\d)',r'\2/\3/\1',date_today_str)
  26.     date_three_days_later_str           = re.sub(r'(\d\d\d\d)-(\d\d)-(\d\d)',r'\2/\3/\1',date_three_days_later_str)
  27.     #document                            = word_app.Documents.Open(FileName = document_name)
  28.     #word_app.Selection.Find.ClearFormatting()  
  29.     #word_app.Selection.Find.Replacement.ClearFormatting()
  30.     for root,dirs,files in os.walk('.'):
  31.         hex_list               = [f for f in os.listdir('.') if f.endswith('.rar')]
  32.     for hex_file in hex_list:
  33.         pack_name = re.sub('.rar','',hex_file)
  34.         document                            = word_app.Documents.Open(FileName = document_name)
  35.         word_app.Selection.Find.ClearFormatting()  
  36.         word_app.Selection.Find.Replacement.ClearFormatting()
  37.         word_app.Selection.Find.Execute('pack_name', False, False, False, False, False, True, 1, True, pack_name, 2)
  38.         word_app.Selection.Find.Execute('today', False, False, False, False, False, True, 1, True, date_today_str, 2)
  39.         if enw_document_type == 'E':
  40.             word_app.Selection.Find.Execute('Three_days_later', False, False, False, False, False, True, 1, True, '', 2)
  41.         else :
  42.             word_app.Selection.Find.Execute('Three_days_later', False, False, False, False, False, True, 1, True, date_three_days_later_str, 2)
  43.         document.SaveAs(pwd + '\\' + 'CTENW_' + pack_name +'.docx')
  44.     #document.Close()
  45.     word_app.Documents.Close()
  46.     word_app.Quit()

  47. enw_documents_generate('ENW_temp.docx')
  48. print "All the ENW documents has been generated!"
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP