Skip to content

Fix UnboundLocalError due to undefined value variable#19

Open
thiagovscoelho wants to merge 1 commit into
snoopyjc:masterfrom
thiagovscoelho:fix_valign_error
Open

Fix UnboundLocalError due to undefined value variable#19
thiagovscoelho wants to merge 1 commit into
snoopyjc:masterfrom
thiagovscoelho:fix_valign_error

Conversation

@thiagovscoelho
Copy link
Copy Markdown

I was getting this error so I made this change that fixes the error. Please review this change and merge if appropriate.

@snoopyjc
Copy link
Copy Markdown
Owner

Thanks!! Can you supply a test case?

@thiagovscoelho
Copy link
Copy Markdown
Author

thiagovscoelho commented Mar 16, 2023

Hi, sure. This is my first time contributing to a project, so please tell me if this isn’t what you wanted. I was running this script, which I made to convert every single xls file in a given folder to xlsx (and then send the originals to the Recycle Bin):

# Import necessary modules
import os  # import os module for interacting with the file system
from xls2xlsx import XLS2XLSX  # import XLS2XLSX class from the xls2xlsx module
import winshell  # import winshell module for interacting with the Windows Shell

# Function to convert xls files to xlsx and send the original xls files to the Recycle Bin
def convert_and_delete_xls_files(folder_path):
    for filename in os.listdir(folder_path):  # loop through each file in the specified folder
        file_path = os.path.join(folder_path, filename)  # create the full file path by joining the folder path and filename
        file_extension = os.path.splitext(filename)[1]  # get the file extension

        if file_extension.lower() == '.xls':  # if the file is an xls file
            xls_file_path = file_path  # set the xls file path to the full file path
            xlsx_file_path = os.path.splitext(file_path)[0] + '.xlsx'  # create the xlsx file path by replacing the extension of the full file path with '.xlsx'

            # Convert xls to xlsx using the XLS2XLSX class
            x2x = XLS2XLSX(xls_file_path)  
            x2x.to_xlsx(xlsx_file_path)

            # Send xls file to trash using the winshell module
            winshell.delete_file(xls_file_path, no_confirm=True, allow_undo=True)

if __name__ == '__main__':
    folder_path = input('Enter the folder path containing .xls files: ')  # prompt the user to enter the folder path containing .xls files
    convert_and_delete_xls_files(folder_path)  # call the convert_and_delete_xls_files function with the specified folder path

This made me see the following output on the console:

Enter the folder path containing .xls files: C:\Users\Usuário\Documents\Thiago\Arquivos\Geral\2023-03-16\teste
Traceback (most recent call last):
  File "C:\Users\Usuário\Documents\Thiago\Arquivos\Utilidades\converter_xls_em_pasta_para_xlsx.py", line 25, in <module>
    convert_and_delete_xls_files(folder_path)  # call the convert_and_delete_xls_files function with the specified folder path
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Usuário\Documents\Thiago\Arquivos\Utilidades\converter_xls_em_pasta_para_xlsx.py", line 18, in convert_and_delete_xls_files
    x2x.to_xlsx(xlsx_file_path)
  File "C:\Users\Usuário\AppData\Roaming\Python\Python311\site-packages\xls2xlsx\xls2xlsx.py", line 190, in to_xlsx
    return self.h2x.to_xlsx(filename=filename)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Usuário\AppData\Roaming\Python\Python311\site-packages\xls2xlsx\htmlxls2xlsx.py", line 1701, in to_xlsx
    if value == 'middle':
       ^^^^^
UnboundLocalError: cannot access local variable 'value' where it is not associated with a value

That is, until I made the edit to htmlxls2xlsx.py that I just committed, which made my code work fine and convert my xls files.

@thiagovscoelho
Copy link
Copy Markdown
Author

thiagovscoelho commented Mar 16, 2023

Update: I thought for a while and noticed that I should probably give you the xls files I was working with. To be sure, I notice now that it wasn’t just any Excel file, it was xls files of bank statements generated by Banco Itaú. Since they have sensitive company information, I’m working on some way to give you one to look at. If I edit them and save them again, Excel converts them to a non-problematic xls, so I can’t easily redact the content.

@thiagovscoelho
Copy link
Copy Markdown
Author

Alright, it took me a lot of work but I managed to make a test file that reproduces the behavior that I was seeing, and which contains no sensitive information because I have scrambled every letter and number, all the while without actually opening the file in Excel. I used a hex editor and parsed it, basically.

The output of the script does look off, stylistically, compared to the result of opening the file in Excel directly. This is a behavior that also happened with the original unexpunged file, and I’m not sure how to handle it right now. But my commit at least makes the program handle this file without an error.

Scrambled-file.xls

XieJiSS added a commit to XieJiSS/xls2xlsx that referenced this pull request Jan 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants