Skip to content

Commit 7eb653a

Browse files
authored
Merge pull request #204 from odhyp/xls_to_xlsx
Add xls_to_xlsx
2 parents c502b72 + 0733c09 commit 7eb653a

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed

IMG/xls_to_xlsx.png

24.5 KB
Loading

xls_to_xlsx/README.md

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
![Star Badge](https://img.shields.io/static/v1?label=%F0%9F%8C%9F&message=If%20Useful&style=style=flat&color=BC4E99)
2+
![Open Source Love](https://badges.frapsoft.com/os/v1/open-source.svg?v=103)
3+
![Python Version](https://img.shields.io/pypi/pyversions/pywin32)
4+
![pywin32 Version](https://img.shields.io/pypi/v/pywin32?label=pywin32)
5+
[![View My Profile](https://img.shields.io/badge/View-My_Profile-green?logo=GitHub)](https://github.com/odhyp)
6+
7+
8+
# XLS TO XLSX
9+
10+
<div align="center">
11+
<a href="https://commons.wikimedia.org/wiki/File:.xlsx_icon.svg">
12+
<img src="https://upload.wikimedia.org/wikipedia/commons/f/f3/.xlsx_icon.svg" width=30% height=30%>
13+
<p><i>Icon Author: Created c. 2018 by
14+
<a href="https://en.wikipedia.org/wiki/Microsoft">Microsoft Corporation</a>
15+
</i></p>
16+
</a>
17+
</div>
18+
19+
## 🛠️ Description
20+
A simple Python script that converts Microsoft Excel '.xls' file to '.xlsx' file.
21+
22+
## ⚙️ Languages or Frameworks Used
23+
This script requires [Python 3](https://www.python.org/downloads/) and [pywin32](https://pypi.org/project/pywin32/).
24+
25+
## 🌟 How to run
26+
27+
1. Ensure you have [Python 3](https://www.python.org/downloads/) installed on your system.
28+
2. Navigate to this project directory or where `xls_to_xlsx.py` is saved.
29+
3. To install the required dependencies, open a terminal and run:
30+
31+
```bash
32+
pip install -r requirements.txt
33+
```
34+
35+
4. Make sure you close all running Microsoft Excel applications before executing the script.
36+
5. To run the script, open a terminal and run the following command:
37+
38+
```bash
39+
python xls_to_xlsx.py
40+
```
41+
42+
6. Follow the prompts to input the file path.
43+
7. The output file is then saved in the same directory as the input file.
44+
8. Input either 'y' to delete the old '.xls' file or 'n' to keep it instead.
45+
46+
## 📺 Demo
47+
<p align="center">
48+
<img src="https://github.com/ndleah/python-mini-project/blob/main/IMG/xls_to_xlsx.png" width=70% height=70%>
49+
50+
## 🤖 Author
51+
[odhy](https://github.com/odhyp)

xls_to_xlsx/requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pywin32==306

xls_to_xlsx/xls_to_xlsx.py

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import os
2+
from pathlib import Path
3+
from win32com.client import Dispatch
4+
5+
6+
def convert_xls_to_xlsx(file_path: str, file_format: int = 51):
7+
"""Convert an Excel file from '.xls' to '.xlsx' format.
8+
9+
Args:
10+
- file_path (str): The path to the input '.xls' file.
11+
- file_format (int, optional): The file format code for '.xlsx'.
12+
Default is 51.
13+
"""
14+
excel_app = Dispatch("Excel.Application")
15+
excel_app.Visible = False
16+
output_path = str(file_path) + 'x'
17+
workbook = excel_app.Workbooks.Open(file_path)
18+
workbook.SaveAs(output_path, FileFormat=file_format)
19+
workbook.Close()
20+
excel_app.Quit()
21+
22+
23+
def remove_old_file(file_path: str):
24+
"""Delete the old 'xls' file.
25+
26+
Args:
27+
- file_path (str): The path to the old 'xls' file.
28+
"""
29+
Path(file_path).unlink(missing_ok=False)
30+
31+
32+
def main():
33+
file_path = str(input("Input the '.xls' file path:\n"))
34+
convert_xls_to_xlsx(file_path=file_path)
35+
36+
file_name = os.path.basename(file_path)
37+
print(f"Successfully converts {file_name}")
38+
39+
is_delete = str(input(
40+
f"Do you want to delete the old {file_name} file (y/n)? ")).lower()
41+
42+
if is_delete == 'y':
43+
remove_old_file(file_path=file_path)
44+
print(f"Successfully removes {file_name}")
45+
else:
46+
pass
47+
48+
49+
if __name__ == '__main__':
50+
main()

0 commit comments

Comments
 (0)