Python operations word "bulk build articles"

Source: Internet
Author: User

Http://www.cnblogs.com/codex/p/4668396.html

Need to make some minutes of the meeting. How many are there in total? Five locations X7 month x 4 articles per month = 140 articles. It's not very important, but the 140 records are completely identical and not good. In general, this type of record is broadly divided into four segments. Then decided to provide four options per paragraph, each paragraph from the four options randomly selected, pieced together into four paragraphs of text, saved into a file. and to print it out, so you're ready to generate a 140-page Word document, one per page.

Need to use the win32com module (download link: http://sourceforge.net/projects/pywin32/files/),

Let Python make a connection to word through COM. The code is as follows:

#-*-coding:cp936-*-#导入随机数模块import random# import win32com module to operate Wordimport win32com from win32com.client import Dispatch, C Onstants# creates a new Word document W = win32com.client.Dispatch (' word.application ') w.visible = 0#0 represents the operation in the background. Set to 1 to see the word interface on the front. W.displayalerts = 0# does not show warning doc = W.documents.add () #准备对文档头部进行操作myRange = doc. Range (0,0) #从第0行第0个字开始: myRange.Style.Font.Name = "Arial" #设置字体myRange. Style.Font.Size = "three" #设置为三号 #======== below is the content part of the article = = ===== #文章标题 (use \ n to control text wrapping) title= ' xxxxx meeting time: ' #会议时间timelist =[' January 9 ', ' January 16 ', ' January 23 ', ' January 30 ', ' February 6 ', ' February 13 ', ' February 20th ', ' February 27 ', ' March 6 ', ' March 13 ', ' March 20 ', ' March 27 ', ' April 3 ', ' April 10 ', ' April 17 ', ' April 24 ', ' May 8 ', ' May 15 ', ' 5 22nd ', ' May 29 ', ' June 5 ', ' June 12 ', ' June 19 ', ' June 26 ', ' July 3 ', ' July 10 ', ' July 17 ', ' July 24 ' #会议地点addrlist =[' \ n Meeting Location: Location axxx\n Host: Zhang X\n ', ' \ n Meeting Venue: Location bxxxx Moderator: Wu x\n ', ' \ n Meeting Venue: Location cxxxx\n Moderator: Wang x\n ', ' \ n Meeting Venue: Location D xxxx\n Moderator: Ran x\n ', ' \ n Meeting place: Location exxxx\n Moderator: Li x\n ',] #参加人员member = ' participants: xxx,xxx,xxx,xXx,xxx,xxx,xxx. \ nthe meeting content: \ n ' #四段文字list1 =[' first paragraph (type a) \ n ', ' first paragraph (type B) \ n ', ' first paragraph (type C) \ n ', ' first paragraph (type D) \ n ']list2=[' second paragraph (type a) \ n ', ' second paragraph (type B) \ n ', ' second paragraph n ', ' the second paragraph (type D) \ n ']list3=[' third paragraph (type a) \ n ', ' third paragraph (type B) \ n ', ' third paragraph (type C) \ n ', ' the third paragraph (type D) \ n ']list4=[' fourth paragraph (type a) \ n ', ' fourth paragraph (type B) \ n '         n ', ' fourth (type D) \ n '] #开始循环操作, write text into Word #先开始遍历地点 (A,b,c,d,e four regions) for addr in addrlist: #遍历28个日期 for Time in Timelist: #随机生成四个数 (range 0-3) aa=random.randint (0,3) bb=random.randint (0,3) cc=random.randint (0,3) d        D=random.randint (0,3) #从文件开头依次插入标题, time, place, people myrange.insertafter (title) Myrange.insertafter Myrange.insertafter (addr) myrange.insertafter (STR3) #在后面继续添加随机选取的四段文字 Myrange.insertafter (LIST1[AA ]) Myrange.insertafter (LIST2[BB]) Myrange.insertafter (LIST3[CC]) Myrange.insertafter (LIST4[DD]) #循环完毕, Save As D:\d.docdoc.SaveAs (R ' D:\d.doc ') #退出操作doc. Close () W.quit ()

Final result

================================================================

==============================================================

Written at the end:

Due to the relatively hasty writing, so some of the details of the problem can not be solved, took 20 minutes to manually adjust a bit. Feel some humiliation. The problem is as follows:

1. The text is a font number third, so the font size at range is set to "16". You want the title to be a second-character, centered display.

2. How do I automatically insert a page break after I finish writing in the fourth paragraph? In this way, each article is printed with a separate page, not "the title of the 2nd article is immediately behind the bottom of the 1th article, printed on the same piece of paper" case.

How to? Solving!

Python operations word "bulk build articles"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.