Add utility scripts for DOC to DOCX conversion and CSV file compression with win32com-based Word automation and batch ZIP archiving functionality
Browse files- doctodocx.py +114 -0
- ziptool.py +19 -0
doctodocx.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import time
|
| 3 |
+
|
| 4 |
+
import win32com.client as win32
|
| 5 |
+
from win32com.client import constants
|
| 6 |
+
|
| 7 |
+
### delete contais of C:\Users\David\AppData\Local\Temp\gen_py if error module 'win32com.gen_py.00020905-0000-0000-C000-000000000046x0x8x7' has no attribute 'MinorVersion' occurs
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def convert_doc_to_docx(doc_path, docx_path=None):
|
| 11 |
+
"""
|
| 12 |
+
Convert a .doc file to .docx format.
|
| 13 |
+
|
| 14 |
+
Args:
|
| 15 |
+
doc_path (str): Path to the input .doc file
|
| 16 |
+
docx_path (str, optional): Path to save the output .docx file.
|
| 17 |
+
If None, saves to same directory with .docx extension.
|
| 18 |
+
Returns:
|
| 19 |
+
str: Path to the converted file
|
| 20 |
+
"""
|
| 21 |
+
# If output path not specified, create one with .docx extension
|
| 22 |
+
if docx_path is None:
|
| 23 |
+
base = os.path.splitext(doc_path)[0]
|
| 24 |
+
docx_path = base + ".docx"
|
| 25 |
+
|
| 26 |
+
# Make sure paths are absolute
|
| 27 |
+
doc_path = os.path.abspath(doc_path)
|
| 28 |
+
docx_path = os.path.abspath(docx_path)
|
| 29 |
+
|
| 30 |
+
try:
|
| 31 |
+
# Create Word application object
|
| 32 |
+
word = win32.gencache.EnsureDispatch("Word.Application")
|
| 33 |
+
word.Visible = False # Run Word in background
|
| 34 |
+
|
| 35 |
+
# Open the DOC file
|
| 36 |
+
doc = word.Documents.Open(doc_path)
|
| 37 |
+
|
| 38 |
+
# Save as DOCX (file format constant is 16 for docx)
|
| 39 |
+
doc.SaveAs(docx_path, FileFormat=constants.wdFormatXMLDocument)
|
| 40 |
+
|
| 41 |
+
# Close the document and quit Word
|
| 42 |
+
doc.Close(False)
|
| 43 |
+
word.Quit()
|
| 44 |
+
|
| 45 |
+
print(f"Successfully converted: {doc_path} -> {docx_path}")
|
| 46 |
+
return docx_path
|
| 47 |
+
|
| 48 |
+
except Exception as e:
|
| 49 |
+
print(f"Error converting {doc_path}: {str(e)}")
|
| 50 |
+
return None
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def convert_folder(input_folder, output_folder=None):
|
| 54 |
+
"""
|
| 55 |
+
Convert all DOC files in a folder to DOCX format.
|
| 56 |
+
|
| 57 |
+
Args:
|
| 58 |
+
input_folder (str): Path to folder containing .doc files
|
| 59 |
+
output_folder (str, optional): Folder to save converted files.
|
| 60 |
+
If None, saves to same folder as input.
|
| 61 |
+
"""
|
| 62 |
+
# Validate input folder
|
| 63 |
+
if not os.path.isdir(input_folder):
|
| 64 |
+
print(f"Error: Input folder does not exist: {input_folder}")
|
| 65 |
+
return
|
| 66 |
+
|
| 67 |
+
# Create output folder if specified
|
| 68 |
+
if output_folder and not os.path.exists(output_folder):
|
| 69 |
+
os.makedirs(output_folder)
|
| 70 |
+
|
| 71 |
+
# Get all DOC files in input folder
|
| 72 |
+
doc_files = [
|
| 73 |
+
f
|
| 74 |
+
for f in os.listdir(input_folder)
|
| 75 |
+
if f.lower().endswith(".doc") and os.path.isfile(os.path.join(input_folder, f))
|
| 76 |
+
]
|
| 77 |
+
|
| 78 |
+
if not doc_files:
|
| 79 |
+
print("No DOC files found in the input folder.")
|
| 80 |
+
return
|
| 81 |
+
|
| 82 |
+
print(f"Found {len(doc_files)} DOC files to convert.")
|
| 83 |
+
|
| 84 |
+
# Convert each file
|
| 85 |
+
success_count = 0
|
| 86 |
+
for doc_file in doc_files:
|
| 87 |
+
input_path = os.path.join(input_folder, doc_file)
|
| 88 |
+
|
| 89 |
+
if output_folder:
|
| 90 |
+
output_path = os.path.join(
|
| 91 |
+
output_folder, os.path.splitext(doc_file)[0] + ".docx"
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
else:
|
| 95 |
+
output_path = None # Let convert_doc_to_docx handle it
|
| 96 |
+
|
| 97 |
+
if convert_doc_to_docx(input_path, output_path):
|
| 98 |
+
success_count += 1
|
| 99 |
+
# wait 1 second
|
| 100 |
+
time.sleep(1)
|
| 101 |
+
|
| 102 |
+
print(
|
| 103 |
+
f"\nConversion complete. Successfully converted {success_count} of {len(doc_files)} files."
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
# Example usage
|
| 108 |
+
if __name__ == "__main__":
|
| 109 |
+
input_folder = (
|
| 110 |
+
r"C:\Users\David\Downloads\TR_ FN4B" # Change this to your folder path
|
| 111 |
+
)
|
| 112 |
+
output_folder = r"C:\Users\David\Downloads\TR_ FN4B\Nouveau dossier" # Optional - set to None to save in same folder
|
| 113 |
+
|
| 114 |
+
convert_folder(input_folder, output_folder)
|
ziptool.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import zipfile
|
| 3 |
+
|
| 4 |
+
# Path to your folder containing CSV files
|
| 5 |
+
folder_path = r"C:\Users\David\Documents\DISK E\BI REPORTING\FLOYD REPORT\2025\CAPACITY\DECEMBRE\20151214"
|
| 6 |
+
|
| 7 |
+
# Loop through all files in the folder
|
| 8 |
+
for filename in os.listdir(folder_path):
|
| 9 |
+
if filename.endswith(".csv"):
|
| 10 |
+
csv_path = os.path.join(folder_path, filename)
|
| 11 |
+
|
| 12 |
+
# Create a .zip file with the same name
|
| 13 |
+
zip_filename = os.path.splitext(filename)[0] + ".zip"
|
| 14 |
+
zip_path = os.path.join(folder_path, zip_filename)
|
| 15 |
+
|
| 16 |
+
with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zipf:
|
| 17 |
+
zipf.write(csv_path, arcname=filename)
|
| 18 |
+
|
| 19 |
+
print(f"Zipped: {csv_path} → {zip_path}")
|