import warnings warnings.simplefilter(action='ignore', category=UserWarning) または↓でもよい openpyxl.reader.excel.warnings.simplefilter('ignore')
import win32com.client app = win32com.client.Dispatch("Excel.Application") app.Visible = False app.DisplayAlerts = False wb = app.Workbooks.Open(exel_file_name) wb.SaveAs(exel_file_name) app.Quit()
import win32com.client from win32com.client import CDispatch from pathlib import Path def proc_xls(fnam:str) -> None: app:CDispatch = win32com.client.Dispatch("Excel.Application") print(type(app)) app.DisplayAlerts = False workbook = app.Workbooks.Open(Path(fnam).resolve()) #Excel定数 EXCEL_xlon = 1 EXCEL_xlOff = -4146 for chk in workbook.Worksheets("個別契約").Checkboxes: print(chk.Name + " のCheck状態は " + str(int(chk.Value) == EXCEL_xlon)) workbook.Close() app.DisplayAlerts = True app.Quit()
参考:C#によるExcel制御
@classmethod def initExcelApp(cls) -> None: cls.__app = win32com.client.Dispatch("Excel.Application") cls.__app.Visible = False cls.__app.DisplayAlerts = False @classmethod def termExcelApp(cls) -> None: if cls.__app is not None: cls.__app.Visible = True cls.__app.DisplayAlerts = True if cls.__app.Workbooks.Count == 0: cls.__app.Quit() del cls.__app # 数式を反映させるために実Excelで開いてセーブする @classmethod def exel_update(cls, exel_file_name:str) -> None: cwd : str = os.getcwd() path:str = cwd + '\\' + exel_file_name wbs = cls.__app.Workbooks wb = wbs.Open(path) wb.Save() wb.Close(SaveChanges=True) wb = None del wb # wbs.Close() # wbs = None # del wbs
@classmethod def initExcelApp(cls) -> None: cls.__app = win32com.client.Dispatch("Excel.Application") pythoncom.CoInitializeEx(pythoncom.COINIT_APARTMENTTHREADED) @classmethod def termExcelApp(cls) -> None: if cls.__app is not None: # excel が開いているかどうかを確認し、起動している場合はQuitする if cls.__app.Workbooks.Count == 0: cls.__app.Quit() pythoncom.CoUninitialize() del cls.__app
# アプリの最初に一回呼ぶ @classmethod def initExcelApp(cls) -> None: cls.__app = win32com.client.Dispatch("Excel.Application") pythoncom.CoInitializeEx(pythoncom.COINIT_APARTMENTTHREADED) cls.__app.Interactive = False cls.__app.ScreenUpdating = False cls.__app.DisplayAlerts = False cls.__app.Visible = False # アプリの最後に一回呼ぶ @classmethod def termExcelApp(cls) -> None: if cls.__app is not None: cls.__app.Visible = True cls.__app.DisplayAlerts = True cls.__app.ScreenUpdating = True cls.__app.Interactive = True # excel が開いているかどうかを確認し、何もないならQuitする if cls.__app.Workbooks.Count == 0: cls.__app.Quit() pythoncom.CoUninitialize() del cls.__app