From f082ebadab8ad2f9c4f7300106a7678dbba2cb6c Mon Sep 17 00:00:00 2001 From: w-8 <8780655+w-8@user.noreply.gitee.com> Date: Tue, 24 May 2022 12:37:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=96=87=E4=BB=B6=E5=AF=B9?= =?UTF-8?q?=E8=B1=A1=E5=88=B0excel?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contributors/FileToExcel/FileToExcel.py | 38 +++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 contributors/FileToExcel/FileToExcel.py diff --git a/contributors/FileToExcel/FileToExcel.py b/contributors/FileToExcel/FileToExcel.py new file mode 100644 index 0000000..9ae3c56 --- /dev/null +++ b/contributors/FileToExcel/FileToExcel.py @@ -0,0 +1,38 @@ +from win32com.client import Dispatch, constants +import win32com + + + +def file_to_excel(excel_path:str,file_path:str): + ''' + 将文件添加到excel对象 + ''' + xlApp = win32com.client.Dispatch('Excel.Application') + xlBook_1 = xlApp.Workbooks.Open(excel_path) + + xlSheet_1 = xlBook_1.Worksheets(1) + xlSheet_1.Shapes.AddOLEObject(Filename=file_path,Link=False)#,DisplayAsIcon=True + xlBook_1.Close(True) + + xlApp.Quit() + + +if __name__=="__main__": + #该主函数脚本为批量添加对象文件到excel + import sys + print(sys.argv) + try: + argv_list = sys.argv + argv_list.pop(0) + excel_path ="" + for idx in argv_list: + if ".xlsx" or ".xls" in idx: + excel_path = idx + break + argv_list.remove(excel_path) + [file_to_excel(excel_path,idy) for idy in argv_list] + except Exception as e: + print(e) + finally: + input("done..") + \ No newline at end of file -- Gitee