Python copies the Word content and USES the formatting font and size instance code

  • 2020-07-21 08:55:57
  • OfStack

Introduction to the

Online parts of the circulation can be baidu keyword "Python" and "word" to view the article after learning, the following content for personal practice, fixed can not run the wrong situation.

Code sample


import win32com 
from win32com.client import Dispatch,constants 
 
w = win32com.client.Dispatch('Word.Application') 
#  Or use the following method to start a separate process:  
# w = win32com.client.DispatchEx('Word.Application') 
#win32com.client.gencache.EnsureDispatch('Word.Application') 
 
#  Background run, no display, no warning  
w.Visible = 0 
w.DisplayAlerts = 0 
 
#  Open the 1 Copy file to new file  
doc = w.Documents.Open( FileName = r'd:\zhengmin.doc' ) 
newdoc = w.Documents.Add() #  Create a new document  
 
 
#  Copy text with insert  
myRange = newdoc.Range(0,0) 
myRange.InsertAfter(doc.Content) # will doc  Copied to the newdoc 
myRange=newdoc.Range(newdoc.Content.Start,newdoc.Content.End) # Future generations  
 
#select=myRange.Select()   # Online learning select Format, actually, the following is correct  
 
myRange.Style.Font.Name=" Regular script "  # Set the font  
myRange.Style.Font.Color=0x0000ff # Set the color of the word  
myRange.Style.Font.Size=30   # Set the size of the word , I guess this function  
 
 
newdoc.SaveAs(r'D:\b.doc')   # Save a new file as b.doc 
newdoc.Close() 
 
#  Shut down  
# doc.Close() 
w.Documents.Close() 
w.Quit() 

conclusion

That's it for the Python example code to copy Word content and use formatting fonts and sizes, hopefully to help you out. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out. Thank you for your support!


Related articles: