如何将字符串中的 %20 转换为 space?
How to transform %20 to space in sring?
我从 tkinter 对话框中获取一个字符串 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()
打印显示正常 space 但是当我尝试注册 pdf 时,它将所有 space 转换为 %20:
例如:“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)
所以我的假设是 tkinter.filedialog.asksaveasfilename return 一些使用 %20 而不是 space 的奇怪编码。
我试过“r”“b”编码(“utf-8”)解码(“utf-8”)html.unescape(pdf_path)urllib.parse.unquote(pdf_path) replace ("%20", " ") 我真的哭得很厉害,因为今天是星期天,我因为这个错误度过了下午 :s
这是一个肮脏的解决方案,但它有效...
如果名字很奇怪,你重命名文件...
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)
我从 tkinter 对话框中获取一个字符串 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()
打印显示正常 space 但是当我尝试注册 pdf 时,它将所有 space 转换为 %20: 例如:“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)
所以我的假设是 tkinter.filedialog.asksaveasfilename return 一些使用 %20 而不是 space 的奇怪编码。
我试过“r”“b”编码(“utf-8”)解码(“utf-8”)html.unescape(pdf_path)urllib.parse.unquote(pdf_path) replace ("%20", " ") 我真的哭得很厉害,因为今天是星期天,我因为这个错误度过了下午 :s
这是一个肮脏的解决方案,但它有效... 如果名字很奇怪,你重命名文件...
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)