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!