'Batch conversion of xlsx files to txt in Python

I am trying to convert files with the extension xlxs to txt files. All items have the same name and are marked with a number. The problem is that there are no numbers in sequence but there are gaps. I have tried structuring the code but it crashes after the first line.

import numpy as np
import pandas as pd

for i in range (1,317,1):
    try:
        df = pd.read_excel(r'D:\Tesi\VAIA\Algoritmo\Tri_%i.xlsx' %i, sheet_name='Tri_%i' %i)
        with open(r'D:\Tesi\VAIA\Algoritmo\Nuvole_split\Nuvola%i.txt' %i, 'w') as outfile:
                df.to_string(outfile)
    except Exception:
        pass

There may be 317 possible output files, but in practice I only have 40. Numbered in ascending order I know that there are: 2, 6, 9, 26 .... . So I'm setting up a loop to open and convert them but it crashes after converting the one in position 2. Any suggestions?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source