最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to solve Python error?: pywintypes.com_error exception that I use for changing the location an excel file - Stack Overflow

programmeradmin7浏览0评论

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_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Excel', 'SaveAs method of Workbook class failed', 'xlmain11.chm', 0, -2146827284), None)

I thought there something wrong with my connection. So I just restart the wifi and try again but still the same error.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论