from win32com.client import Dispatch
import time
myWord = Dispatch('Word.Application')
myWord.Visible = 1 # comment out for production
url = 'http://127.0.0.1:8000/t/%E6%96%B0%E9%97%BB/1.html'
# 用word打開網頁最大限度保持格式
myWord.Documents.Open(url) # open file
doc = myWord.ActiveDocument
rngPage = doc.Bookmarks("\Page").Range
page_count = doc.ActiveWindow.Panes(1).Pages.Count
print('pagecount', page_count)
# time.sleep(1000)
#全選
myWord.Selection.WholeStory()
# 復制
myWord.Selection.Copy()
doc_add = myWord.Documents.Open(r"C:\Users\Administrator\Documents\1.docx")
wdRange = doc_add.Content
print(doc.Name)
# 激活 doc_add
myWord.Documents(doc_add).Activate()
doc = myWord.ActiveDocument
print(doc.Name)
#復制粘貼兩次
myWord.Selection.Paste()
myWord.Selection.Paste()
myWord.ActiveDocument.SaveAs('172.docx',16)
myWord.Quit() # close Word.Application
參考
官方文檔
https://docs.microsoft.com/en-us/office/vba/api/word.words
https://stackoverflow.com/questions/15274601/opening-a-ms-word-file-in-pywin32
https://www.cnblogs.com/zhuminghui/p/11765401.html