0

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

  • Maybe [`from urllib.parse import unquote; unquote(pdf_path)`](https://stackoverflow.com/a/16566128/3439404)? – JosefZ Dec 06 '20 at 16:42
  • @JosefZ I tried it to, doesn't work either :/ –  Dec 06 '20 at 16:51
  • _"So my assumption is that tkinter.filedialog.asksaveasfilename return some weird encoding with %20 instead of space."_ - no, the dialog doesn't do that. That is a very easy thing to test. – Bryan Oakley Dec 06 '20 at 17:14
  • Sounds like the string was "url-encoded" when it should not have been. – Rick James Dec 06 '20 at 22:46

1 Answers1

0

This is a dirty solution but it works... If the name is weird, you rename the file...

from os.path import isfile
from os import rename
if not isfile(pdf_path):
    print("the weird problem of space becoming %20")
    weird_pdf_path = pdf_path.replace(" ","%20")
    if isfile(weird_pdf_path):
        rename(weird_pdf_path, pdf_path)