Having problems with Python, OpenSCAD and solidpython, when trying to use special characters, as output

47 views Asked by At

so im writing a program to automate making items with names on them, and im encountering a problem when trying to render the models with special characters ž đ š ć č, where it cant translate them i guess and it just ends up crashing. ill post the code here and explain a bit within it.

This is part of the program, where the issue is 

import pandas as pd
from solid import *
import subprocess
import time

if name == ‘main’:
# Read the Excel file

df = pd.read_excel(r"C:\Users\Miki\Desktop\data.xlsx" ,sheet_name='Sheet1')
#print(df.iterrows)
num_rows = len(df)
#print(df)
#print(df.shape)
# Iterate through each row of the Excel sheet
for index, row in df.iterrows():
# Read values from the first row and first three columns
    annotation1 = row.iloc[2] #1st phone number
    annotation2 = row.iloc[0] #Name
    annotation3 = row.iloc[3] #2nd phone number
    annotation4 = row.iloc[1] #Surname
    full_name = f"{annotation2} {annotation4}"
    **#full_name = str(annotation2)+" "+str(annotation4)**
    #print(full_name)
  
    #print(annotation1,annotation2,annotation3,annotation4)
    # Generate the assembly
    a = assembly(annotation1,str(full_name), annotation3)
  
    # Output the SCAD file with dynamic name based on annotation1
    scad_file = f"export_{annotation2}.scad"
    scad_file_location = f"C:\\Users\\Miki\\Desktop\\SCADfiles"
    scad_render_to_file(a, scad_file, include_orig_code=False)

   

    ######### Problem ko hočem renderat file ne gre, ker ima "full_name" presledek in ga unicoda ne sprejme. help
    #Problem when i want to render the file, it doesnt let me since the "full name" has restricted character "\t" and doesnt work then i guess xD

    # Run OpenSCAD to export the STL file
   ** stl_file = f"{annotation2}.stl"
    command = f"openscad -o C:\\Users\\Miki\\Desktop\\STLfiles\\{stl_file} C:\\Users\\Miki\\Desktop\\SCADfiles\\{scad_file}"
    subprocess.run(command, shell=True)
    time.sleep(50)
    print(full_name, "je opravljen.")***
This here is the error given back, as you can see it crashes at scad_render_to_file where var a has a var within itself “full_name” that contains a special char.


This here is the error given back, as you can see it crashes at scad_render_to_file where var a has a var within itself “full_name” that contains a special char.

Traceback (most recent call last):
File “c:\Users\Miki\Desktop\Model_cut_v3.py”, line 87, in
scad_render_to_file(a, scad_file, include_orig_code=False)
File “C:\Users\Miki\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\solid\solidpython.py”, line 518, in scad_render_to_file
return _write_code_to_file(rendered_string, filepath, out_dir, include_orig_code)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\Miki\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\solid\solidpython.py”, line 562, in _write_code_to_file
out_path.write_text(rendered_string)
File “C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.752.0_x64__qbz5n2kfra8p0\Lib\pathlib.py”, line 1048, in write_text
return f.write(data)
^^^^^^^^^^^^^
File “C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.752.0_x64__qbz5n2kfra8p0\Lib\encodings\cp1252.py”, line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeEncodeError: ‘charmap’ codec can’t encode character ‘\u010d’ in position 555: character maps to

So i was wondering, how i can fix this. Is there maybe a way to decode it within OpenSCAD?, though i dont know if thats possible.

Well if anyone has any ideas how to fix this, i would really appriciate it.

I tried encoing it in to ascii, and it resulted in well ascii values for each letter, which is not what i want. I also tried encoing it with utf-8, and had again an not satisfying result. if anyone can help, thanks

1

There are 1 answers

0
Trym30 On
scad_file_location = f"C:\\Users\\Miki\\Desktop\\SCADfiles\\export_{annotation2}.scad"
with open(scad_file_location, "w", encoding="utf8") as f:
    f.write(scad_render(model))