Python copies the Word content and uses the format to set the font and size of the Instance code, pythonword

Source: Internet
Author: User

Python copies the Word content and uses the format to set the font and size of the Instance code, pythonword
Introduction

You can refer to Baidu keywords "Python" and "word" and view the article for further study. The following content is my personal practice, which fixes the issue that cannot run errors.

Sample Code
Import win32com from win32com. client import Dispatch, constants w = win32com. client. dispatch ('word. application ') # or use the following method to start an independent process: # w = win32com. client. dispatchEx ('word. application ') # win32com. client. gencache. ensureDispatch ('word. application ') # Run in the background, not displayed, no warning w. visible = 0 w. displayAlerts = 0 # open a file and copy it to the new file doc = w. documents. open (FileName = r 'd: \ zhengmin.doc ') newdoc = w. documents. add () # create a new document # insert and copy the text myRange = newdoc. range (0, 0) myRange. insertAfter (doc. content) # copy the doc to newdoc myRange = newdoc. range (newdoc. content. start, newdoc. content. end) # select all # select = myRange. select () # I learned how to use the select format on the Internet. In fact, the following applies to myRange. style. font. name = "" # Set the font myRange. style. font. color = 0x0000ff # Set the Color of the word myRange. style. font. size = 30 # Set the font Size. I can deduce this function newdoc. saveAs (r 'd: \ B .doc '). The new file B .doc newdoc is used. close () # Close # doc. close () w. documents. close () w. quit ()
Summary

The above is all about copying the Word content in Python and setting the font and size of the Instance code in the format. I hope this will help you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

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.