[Python] how to solve Python error : pywintypes.com_error exception that i use for changing...

Discussão em 'Python' iniciado por Stack, Março 27, 2025.

  1. Stack

    Stack Membro Participativo

    so I have this code that I used to convert an excel file ( .xls) to (.xlsx) and edit it and save it to another location.. it used to work perfectly but just now I encounter some Error with the xlmain11.chm? I don't understand the problem.. can someone help me?

    this is my shorter code

    import win32com.client as w3c
    import time
    class ConvertAndMoveExcel:
    def __init__(self, year):
    self.year = year
    self.path_from = r"C:\Users\[name]\OneDrive\Queen Wifi\Download Sementara"
    if self.year == 2024:
    self.path_to = r"C:\Users\[name]\OneDrive\Queen Wifi\data E-ebill"
    elif self.year == 2025:
    self.path_to = r"C:\Users\[name]\OneDrive\Queen Wifi\data E-ebill 2025"

    def file_name(self, server, data, month, paths, type):... #just a function to determine the file name location

    def isruning(self):
    proceses = psutil.process_iter()
    for proc in proceses:
    if proc.name() == "EXCEL.EXE":
    proc.kill()
    print("Excel is closed completely") #so I can see the log cause sometimes the excel was not closed properly
    def save_file(self, path_from : Path, path_to : Path):
    self.isruning()
    xl = w3c.Dispatch('Excel.Application')
    wb = xl.Workbooks.Open(path_from)
    xl.DisplayAlerts = False
    wb.SaveAs(path_to, FileFormat=51)
    xl.DisplayAlerts = True
    wb.Close(True)
    xl.Application.Quit()
    self.delete_file(path_from)
    self.isruning()
    def auto_save(self):
    while True:
    month = input("Month: {01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12} or exit: ")
    if month == "exit":
    break
    server = [1391, 2107, 3119]
    data = ["user", "income"]
    for i in server:
    for j in data:
    print(f"Saving Server {i} {j} {month} on progress")
    path_from = str(self.file_name(i,j,month,self.path_from,"xlx"))+"x"
    print(path_from)
    path_to = str(self.file_name(i,j,month,self.path_to,"xlsx"))
    print(path_to)
    if Path(path_from).exists():
    self.save_file(path_from,path_to)
    print(f"File {path_from} saved to {path_to}")
    time.sleep(5)
    else:
    print(f"File {path_from} not found")

    if __name__ == "__main__":
    year = int(input("Enter the year: "))
    convert_excel = ConvertAndMoveExcel(year)
    while True:
    asking = input("Save the file? : y/n ")
    if asking == "y":
    convert_excel.auto_save()
    else:
    break



    the error in cmd

    Month: {01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12} or exit: 03
    Saving Server 1391 user 03 on progress
    C:\Users\[name]\OneDrive\Queen Wifi\Download Sementara\Data Pelanggan 1391.xlsx
    C:\Users\[name]\OneDrive\Queen Wifi\data E-ebill 2025\1391\Download\Data Pelanggan.xlsx
    Excel is closed completely
    Traceback (most recent call last):
    File "C:\Users\[name]\OneDrive\Queen Wifi\finalprogram.py", line 344, in <module>
    convert_excel.setting()
    File "C:\Users\[name]\OneDrive\Queen Wifi\finalprogram.py", line 327, in setting
    self.auto_save()
    File "C:\Users\[name]\OneDrive\Queen Wifi\finalprogram.py", line 264, in auto_save
    self.save_file(path_from,path_to)
    File "C:\Users\[name]\OneDrive\Queen Wifi\finalprogram.py", line 217, in save_file
    wb.SaveAs(path_to, FileFormat=51)
    File "<COMObject Open>", line 4, in SaveAs
    pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Excel', 'SaveAs method of Workbook class failed', 'xlmain11.chm', 0, -2146827284), None)


    so I thought there something wrong with my conection.. so i just restart the wifi and try again but still the same error

    Continue reading...

Compartilhe esta Página