Skip to content

Commit

Permalink
Merge pull request #689 from odhyp/xls_to_xlsx
Browse files Browse the repository at this point in the history
Add new project: xls to xlsx converter
  • Loading branch information
Mrinank-Bhowmick authored Jan 13, 2024
2 parents 25776b5 + 50fde6d commit 416f3f6
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
45 changes: 45 additions & 0 deletions projects/xls_to_xlsx/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
![Python Version](https://img.shields.io/pypi/pyversions/pywin32)
![pywin32 Version](https://img.shields.io/pypi/v/pywin32?label=pywin32)
[![View My Profile](https://img.shields.io/badge/View-My_Profile-green?logo=GitHub)](https://github.com/odhyp)


# XLS TO XLSX

<div align="center">
<a href="https://commons.wikimedia.org/wiki/File:.xlsx_icon.svg">
<img src="https://upload.wikimedia.org/wikipedia/commons/f/f3/.xlsx_icon.svg" width=30% height=30%>
<p><i>Icon Author: Created c. 2018 by
<a href="https://en.wikipedia.org/wiki/Microsoft">Microsoft Corporation</a>
</i></p>
</a>
</div>

## Description
A simple Python script that converts Microsoft Excel '.xls' file to '.xlsx' file.

## Languages or Frameworks Used
This script requires [Python 3](https://www.python.org/downloads/) and [pywin32](https://pypi.org/project/pywin32/).

## How to run

1. Ensure you have [Python 3](https://www.python.org/downloads/) installed on your system.
2. Navigate to this project directory or where `xls_to_xlsx.py` is saved.
3. To install the required dependencies, open a terminal and run:

```bash
pip install -r requirements.txt
```

4. Make sure you close all running Microsoft Excel applications before executing the script.
5. To run the script, open a terminal and run the following command:

```bash
python xls_to_xlsx.py
```

6. Follow the prompts to input the file path.
7. The output file is then saved in the same directory as the input file.
8. Input either 'y' to delete the old '.xls' file or 'n' to keep it instead.

## Author
[odhy](https://github.com/odhyp)
1 change: 1 addition & 0 deletions projects/xls_to_xlsx/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pywin32==306
50 changes: 50 additions & 0 deletions projects/xls_to_xlsx/xls_to_xlsx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import os
from pathlib import Path
from win32com.client import Dispatch


def convert_xls_to_xlsx(file_path: str, file_format: int = 51):
"""Convert an Excel file from '.xls' to '.xlsx' format.
Args:
- file_path (str): The path to the input '.xls' file.
- file_format (int, optional): The file format code for '.xlsx'.
Default is 51.
"""
excel_app = Dispatch("Excel.Application")
excel_app.Visible = False
output_path = str(file_path) + 'x'
workbook = excel_app.Workbooks.Open(file_path)
workbook.SaveAs(output_path, FileFormat=file_format)
workbook.Close()
excel_app.Quit()


def remove_old_file(file_path: str):
"""Delete the old 'xls' file.
Args:
- file_path (str): The path to the old 'xls' file.
"""
Path(file_path).unlink(missing_ok=False)


def main():
file_path = str(input("Input the '.xls' file path:\n"))
convert_xls_to_xlsx(file_path=file_path)

file_name = os.path.basename(file_path)
print(f"Successfully converts {file_name}")

is_delete = str(input(
f"Do you want to delete the old {file_name} file (y/n)? ")).lower()

if is_delete == 'y':
remove_old_file(file_path=file_path)
print(f"Successfully removes {file_name}")
else:
pass


if __name__ == '__main__':
main()

0 comments on commit 416f3f6

Please sign in to comment.