Search code examples
pythonc#visual-studio-2022

Unable to run Python code in Visual Studio 2022


good afternoon,

I have a Python script that runs smoothly in Visual Studio Code

However, when I run it in Visual Studio 2022, the following error appears:

SyntaxError: Non-UTF-8 code starting with '\xe3' in file C:\Users\PythonApp\PythonApp1.py on line 18, but no encoding declared; see https://peps.python.org/pep-0263/ for details

Accessing the help url I am instructed to enter a coding declaration beforehand but it still doesn't help

Is there any configuration that must be done in advance in Visual Studio 2022 to correctly execute the code?

Can anyone give me some guidance?

My Code:

import fitz  # PyMuPDF
import openpyxl
from pptx import Presentation
from docx import Document
import os

def identify_file_type(file_path):
    _, file_extension = os.path.splitext(file_path.lower())
    if file_extension == '.pdf':
        return 'pdf'
    elif file_extension == '.xlsx':
        return 'excel'
    elif file_extension == '.pptx':
        return 'powerpoint'
    elif file_extension == '.docx':
        return 'word'
    else:
        raise ValueError('Tipo de arquivo não suportado.')

def convert_file_to_text(file_path):
    file_type = identify_file_type(file_path)

    if file_type == 'pdf':
        text = ''
        pdf_document = fitz.open(file_path)
        for page_num in range(pdf_document.page_count):
            page = pdf_document[page_num]
            text += page.get_text()
        pdf_document.close()
        return text
    elif file_type == 'excel':
        workbook = openpyxl.load_workbook(file_path)
        text = ''
        for sheet_name in workbook.sheetnames:
            sheet = workbook[sheet_name]
            for row in sheet.iter_rows(values_only=True):
                text += ' '.join(str(cell) for cell in row) + '\n'
        return text
    elif file_type == 'powerpoint':
        presentation = Presentation(file_path)
        text = ''
        for slide in presentation.slides:
            for shape in slide.shapes:
                if hasattr(shape, 'text'):
                    text += shape.text + '\n'
        return text
    elif file_type == 'word':
        doc = Document(file_path)
        text = ''
        for paragraph in doc.paragraphs:
            text += paragraph.text + '\n'
        return text

def main(file_path):
    text = convert_file_to_text(file_path)

    output_file_path = 'C:/Users/rafsouza/Downloads/output.txt'
    with open(output_file_path, 'w', encoding='utf-8') as output_file:
        output_file.write(text)

if __name__ == "__main__":
    input_file_path = input("Digite o caminho completo do arquivo: ")
    main(input_file_path)

I want to be able to use functions from a Python project in another C# project in Visual Studio 2022


Solution

  • Code xE3 is probably 'ã'.

    Make sure that your source code file is stored in Unicode (UTF-8).