콘솔워크

xlsx 파일을 xls로 변환 본문

프로그래밍/python

xlsx 파일을 xls로 변환

이휘재123 2022. 10. 12. 16:52
반응형
import win32com.client as win32

def xlsx_to_xls(source):
    if not str(source).find("xlsx") > -1:
        return

    if not os.path.isfile(source):
        return

    excel = win32.gencache.EnsureDispatch("Excel.Application")
    wb = excel.Workbooks.Open(source)

    xls_file = source[:-1]
    if os.path.isfile(xls_file):
        os.remove(xls_file)
    wb.SaveAs(xls_file, FileFormat=56)  # FileFormat = 51 is for .xlsx extension
    wb.Close()  # FileFormat = 56 is for .xls extension
    excel.Application.Quit()

    os.remove(source)

변환에 사용한 .xlsx 파일은 삭제된다.

반응형