Exemplo n.º 1
0
def PassProtect(Path, Pass):
    from win32com.client.gencache import EnsureDispatch

    xlApp = EnsureDispatch("Excel.Application")
    xlwb = xlApp.Workbooks.Open(Path)
    xlApp.DisplayAlerts = False
    xlwb.Visible = False
    xlwb.SaveAs(Path, Password=Pass)
    xlwb.Close()
    xlApp.Quit()
Exemplo n.º 2
0
 def update_html(self):
     xl = EnsureDispatch('Excel.Application')
     xl.DisplayAlerts = False
     wb = xl.Workbooks.Open(flowloger)
     try:
         wb.SaveAs(self.HTML_DOC_PATH, constants.xlHtml)
     except:
         xl.Workbooks.Close()
         xl.Quit()
         shutil.rmtree(self.HTML_DOC_PATH.split("\\GL_log.htm")[0])
         os.makedirs(self.HTML_DOC_PATH.split("\\GL_log.htm")[0])
         update_html()
         pass
     xl.Workbooks.Close()
     xl.Quit()
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Codes to operate excel application using win32com api.
"""

from PIL import ImageGrab
from win32com.client import constants
from win32com.client.gencache import EnsureDispatch

EXCEL = EnsureDispatch("Excel.application")
EXCEL.Visible = True
EXCEL.DisplayAlerts = False
EXCEL.AstToUpdateLinks = False
EXCEL.EnableEvents = False

workbook = EXCEL.Workbooks.Open("<path to input>.xlsx")
worksheet = workbook.Sheets("Sheet1")

# Getting values from a single cell in Excel.
value = worksheet.Range("A1").Value
print(value)

# Getting last row and column of a table
last_row = worksheet.Range("A1").End(-4121).Row
last_col = worksheet.Range("A1").End(-4161).Column
last_cel = worksheet.Cells(last_row, last_col).Address

# Normal Copy-Paste
worksheet.Range(f"A1:{last_cel}").Copy()
# or
Exemplo n.º 4
0
from win32com.client.gencache import EnsureDispatch
from win32com.client import constants

xl = EnsureDispatch('Excel.Application')
xl.Workbooks.Open('C:\\Users\\Ozgur\\Desktop\\htm\\macro2.xlsm')
xl.DisplayAlerts = False
xl.Application.Run("macro2.xlsm!module1.xx")
xl.Application.Save()
xl.Application.Quit()
del xl