Converting xls files to xlsx using python
Question:
I’ve got loads of 97-2003 Excel xls files i wanna bulk convert to xlsx.
I found the xls2xlsx
documentation but can’t seem to get it to work.
I have tried googling the errors and searching but to no avail.
import os
from xls2xlsx import XLS2XLSX
directory = 'C:\Users\Python Scripts\convertXLStoXLSX\'
for filename in os.listdir(directory):
if filename.endswith(".xls"):
x2x = XLS2XLSX(filename)
x2x.to_xlsx(filename)
else:
continue
I am getting the error message:
ImportError: cannot import name 'GuessedAtParserWarning' from 'bs4' (C:Userswf5931AppDataLocalContinuumanaconda3libsite-packagesbs4__init__.py)
Answers:
use pandas
import pandas as pd
df = pd.read_excel("file.xls")
df.to_excel("file.xlsx")
Just including my finished code for future use by others which uses pandas – worked almost instantly
import os
import pandas as pd
directory = 'C:\Users\Documents\Python Scripts\convertXLStoXLSX\'
for filename in os.listdir(directory):
if filename.endswith(".xls"):
df = pd.read_excel(filename)
df.to_excel(filename + ".xlsx")
I’ve got loads of 97-2003 Excel xls files i wanna bulk convert to xlsx.
I found the xls2xlsx
documentation but can’t seem to get it to work.
I have tried googling the errors and searching but to no avail.
import os
from xls2xlsx import XLS2XLSX
directory = 'C:\Users\Python Scripts\convertXLStoXLSX\'
for filename in os.listdir(directory):
if filename.endswith(".xls"):
x2x = XLS2XLSX(filename)
x2x.to_xlsx(filename)
else:
continue
I am getting the error message:
ImportError: cannot import name 'GuessedAtParserWarning' from 'bs4' (C:Userswf5931AppDataLocalContinuumanaconda3libsite-packagesbs4__init__.py)
use pandas
import pandas as pd
df = pd.read_excel("file.xls")
df.to_excel("file.xlsx")
Just including my finished code for future use by others which uses pandas – worked almost instantly
import os
import pandas as pd
directory = 'C:\Users\Documents\Python Scripts\convertXLStoXLSX\'
for filename in os.listdir(directory):
if filename.endswith(".xls"):
df = pd.read_excel(filename)
df.to_excel(filename + ".xlsx")