-
Notifications
You must be signed in to change notification settings - Fork 698
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #689 from odhyp/xls_to_xlsx
Add new project: xls to xlsx converter
- Loading branch information
Showing
3 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
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
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) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pywin32==306 |
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
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() |