I take a string from a tkinter dialog window:
from tkinter import filedialog
def get_register_path_and_filename(initialfile=""):
filename = filedialog.asksaveasfilename(initialfile=initialfile, title="Register file",
filetypes=[("Excel Files", ".xlsx")])
if filename == "":
print("pas d'enregistrement")
return None
print(filename)
return filename
if __name__ == "__main__":
from tkinter import Tk
app = Tk()
get_register_path_and_filename("totolabrebis")
app.destroy()
The print show normal space BUT when I try to register a pdf, it transform all space into %20: ex: "toto la brebis.pdf" -> "toto%20la%20brebis.pdf"
import win32com.client
def register_pdf(excel_path, tab_to_display_list):
if not excel_path[-5:] == ".xlsx":
print("error excel_to_pdf.py l37:",excel_path,excel_path[-5:])
return None
pdf_path = excel_path[:-5]+".pdf"
print(pdf_path)
try:
excel_obj = win32com.client.Dispatch("Excel.Application")
excel_obj.Visible = False
except Exception as e:
print(e)
print("error excel_to_pdf.py l46")
return None
try:
wb = excel_obj.Workbooks.Open(excel_path)
except Exception as e:
print(e)
print("error excel_to_pdf.py l52")
return None
tab_id_to_display_list = []
for i in range(1,len(wb.Sheets)+1):
tab_name = wb.Sheets(i).Name
if tab_name in tab_to_display_list:
tab_id_to_display_list.append(i)
try:
print([(i,wb.Sheets(i).Name) for i in tab_id_to_display_list])
wb.WorkSheets(tab_id_to_display_list).Select()
wb.ActiveSheet.ExportAsFixedFormat(0, pdf_path, IgnorePrintAreas=True)
wb.Close(True)
excel_obj.Quit()
except Exception as e:
print(e)
print("error excel_to_pdf.py l60")
return None
print("successfully registered pdf")
return pdf_path
if __name__ == "__main__":
excel_path = "D:\\Bureau\\toto 3.xlsx"
tab_to_display_list = ["Feuil1"]
pdf_state = register_pdf(excel_path,tab_to_display_list)
So my assumption is that tkinter.filedialog.asksaveasfilename return some weird encoding with %20 instead of space.
I tried "r" "b" encode("utf-8") decode("utf-8") html.unescape(pdf_path) urllib.parse.unquote(pdf_path) replace ("%20", " ") and I'm actually crying really loud because it's sunday and I passed my afternoon on this error :s