Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to convert xlsx to xls
#1
I have a bunch of .xlsx files that I want to convert to xls, because my code works only with .xls
Could you suggest something for me?
Reply
#2
one method would be to open in excel as .xlsx and save as .xls
Hard to say otherwise since you haven't posted your code
Reply
#3
Yes, but I have more than 100 xlsx so it would keep more times. I thought it would be possible to have a new program that save all xlsx to xls.
Reply
#4
the following code can convert all (requires python 3.6 or newer, and openpyxl):
Replace '.' in line 7 with your path to xlsx files .xls files will be in same directory
You should probably remove the print statement. I only left it so you can see what's there
from pathlib import Path
import openpyxl
import os

# get files
os.chdir(os.path.abspath(os.path.dirname(__file__)))
pdir = Path('.')
filelist = [filename for filename in pdir.iterdir() if filename.suffix == '.xlsx']

for filename in filelist:
    print(filename.name)

for infile in filelist:
    workbook = openpyxl.load_workbook(infile)
    outfile = f"{infile.name.split('.')[0]}.xls"
    workbook.save(outfile)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert .xlsx to Format as Table bnadir55 0 862 Aug-11-2022, 06:39 AM
Last Post: bnadir55
  Convert legacy print file to XLSX file davidm 1 1,768 Oct-17-2021, 05:08 AM
Last Post: davidm

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020