-1

I have .xls excel file. I'm converting it to xlsx by;

df = panda.read_excel(currentPath)
df.to_excel(currentPath+ 'x')

But this messes with the file, columns and rows are changing place, is there a way to convert xls to xlsx without changing anything?

Talha Akca
  • 394
  • 2
  • 8
  • Welcome to [Stack Overflow.](https://stackoverflow.com/ "Stack Overflow") Please be aware this is not a code-writing or tutoring service. We can help solve specific, technical problems, not open-ended requests for code or advice. Please edit your question to show what you have tried so far, and what specific problem you need help with. See the [How To Ask a Good Question](https://stackoverflow.com/help/how-to-ask "How To Ask a Good Question") page for details on how to best help us help you. – itprorh66 Feb 07 '22 at 14:41
  • 2
    pandas itself cannot handle 97-03 xls. it's far more than just the file extension. – Lei Yang Feb 07 '22 at 14:45
  • @LeiYang thanks for the answer, will change the way I handle then – Talha Akca Feb 07 '22 at 14:47

1 Answers1

1

Pandas isn't primarily intended for manipulating Excel files, and XLS and XLSX are separate formats, not just differences in naming convention. Because DataFrames aren't intended to represent either XLS or XLSX format, you're juggling three different formats, and the format change might be in either XLS -> DF or DF -> XLSX. That suggests that you probably should not use Pandas DataFrames for this task. If you need to do other manipulations with Pandas DF, then you can look into reordering DF columns.

Assuming you're really trying to reformat the files and do Excel-style manipulations, I recommend openpyxl, although there are a few competing libraries you might also find useful. (Also, as @lei-yang notes below, openpyxl may not be able to read XLS files - although other packages listed there should be able to do so.)

If you're not doing any manipulations of the file itself, and don't care about having the file names match the internal formats, the Python os and shutil libraries can cover that.

Sarah Messer
  • 3,592
  • 1
  • 26
  • 43
  • 1
    in the page you pasted, `xlrd` and `xlwt` is better than `openpyxl`. because the OP wants to convert xls to xlsx format. – Lei Yang Feb 07 '22 at 15:03