程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Word win32com add attachments through Python small attachments VBA conversion

編輯:Python
import win32com.client as win32
from win32com.client import constants
# import originpro as op
import time
# src_opju = op.path('e')+ r'Samples\Map Data.opju'
# op.open(file = src_opju, readonly = True)
word = win32.gencache.EnsureDispatch('Word.Application')
word_doc = word.Documents.Add()
# gp = op.find_graph('Graph9')
# gp.activate()
# 4 refers to OLE Object
# https://www.originlab.com/doc/COM/Classes/Application/CopyPage
# op.po.CopyPage(gp.name,4)
tic = time.perf_counter()
word_doc.ActiveWindow.Selection.InlineShapes.AddOLEObject(
ClassType='Word.Document.12',
FileName="E:\ Work related \ customer demand .doc",
LinkToFile=True,
DisplayAsIcon=True,
IconFileName= r"C:\Windows\Installer\{90160000-0011-0000-1000-0000000FF1CE}\wordicon.exe",
IconIndex =13,
IconLabel="1.docx"
)
toc = time.perf_counter()
print(f"Adding embedded object took {
toc - tic:0.4f} seconds")
word_doc.SaveAs2(r"D:\KKCap\1.docx")

design sketch


VBA Reference resources

Sub エ building 6()
'
' エ building 6 エ building
'
'
Selection.InlineShapes.AddOLEObject ClassType:="Word.Document.12", _
FileName:="C:\Users\H7113492\Documents\ㄏノ" & ChrW(35828) & " for" & _
ChrW(32418) & ".docx", LinkToFile:=True, DisplayAsIcon:=True, _
IconFileName:= _
"C:\Windows\Installer\{90160000-0011-0000-1000-0000000FF1CE}\wordicon.exe" _
, IconIndex:=13, IconLabel:="ㄏノ" & ChrW(35828) & " for" & ChrW(32418) & _
".docx"
End Sub

Reference code

shape = self.wordDoc.InlineShapes.AddOLEObject(
ClassType='Excel.Chart',
FileName=filename
)
# set height and width in points
shape.Height = height
shape.Width = width
import win32com.client as win32
from win32com.client import constants
import originpro as op
import time
src_opju = op.path('e')+ r'Samples\Map Data.opju'
op.open(file = src_opju, readonly = True)
word = win32.gencache.EnsureDispatch('Word.Application')
word_doc = word.Documents.Add()
gp = op.find_graph('Graph9')
gp.activate()
# 4 refers to OLE Object
# https://www.originlab.com/doc/COM/Classes/Application/CopyPage
op.po.CopyPage(gp.name,4)
tic = time.perf_counter()
word_doc.ActiveWindow.Selection.PasteSpecial(DataType=constants.wdPasteOLEObject)
toc = time.perf_counter()
print(f"Adding embedded object took {
toc - tic:0.4f} seconds")
word_doc.SaveAs2("C:\\test\\test.docx")
op.exit()

Reference resources
https://my.originlab.com/forum/topic.asp?TOPIC_ID=47379

http://www.icodeguru.com/webserver/Python-Programming-on-Win32/ch10.htm


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved