Created
January 10, 2023 02:54
-
-
Save tos-kamiya/bcac717814d65b4e7eb9b2338a499ebe to your computer and use it in GitHub Desktop.
Convert old-format MS Office files (.doc, .xls) into newer format (.docx, .xlsx)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ref: https://stackoverflow.com/questions/66781927/doc-to-docx-conversion-in-python | |
from glob import glob | |
import os | |
import win32com.client as win32 | |
from win32com.client import constants | |
def save_as_docx(path): | |
word = win32.gencache.EnsureDispatch('Word.Application') | |
doc = word.Documents.Open(path) | |
doc.Activate () | |
new_file_abs = os.path.splitext(os.path.abspath(path))[0] + ".docx" | |
word.ActiveDocument.SaveAs( | |
new_file_abs, FileFormat=constants.wdFormatXMLDocument | |
) | |
doc.Close(False) | |
paths = glob(r'.\**\*.doc', recursive=True) | |
for path in paths: | |
path = os.path.abspath(path) | |
print("path=%s" % path) | |
save_as_docx(path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ref: https://stackoverflow.com/questions/9918646/how-to-convert-xls-to-xlsx | |
from glob import glob | |
import os | |
import win32com.client as win32 | |
from win32com.client import constants | |
def save_as_xlsx(path): | |
excel = win32.gencache.EnsureDispatch('Excel.Application') | |
wb = excel.Workbooks.Open(path) | |
new_file_tmp = os.path.splitext(os.path.abspath(path))[0] + "-x.xlsx" | |
if os.path.exists(new_file_tmp): | |
os.remove(new_file_tmp) | |
wb.SaveAs(new_file_tmp, FileFormat=51) | |
wb.Close() | |
excel.Application.Quit() | |
new_file = os.path.splitext(os.path.abspath(path))[0] + ".xlsx" | |
if os.path.exists(new_file): | |
os.remove(new_file) | |
os.rename(new_file_tmp, new_file) | |
paths = glob(r'.\**\*.xls', recursive=True) | |
for path in paths: | |
path = os.path.abspath(path) | |
print("path=%s" % path) | |
save_as_xlsx(path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment