import time import pyperclip """ 安装pyperclip pip install pyperclip """ #此代码可复制pdf中内容,并自动删除换行符和空格 def is_Qnumber(uchar): """判断一个unicode是否是全角数字""" if uchar >= u'\uff10' and uchar <= u'\uff19': return True else: return False def is_Qalphabet(uchar): """判断一个unicode是否是全角英文字母""" if (uchar >= u'\uff21' and uchar <= u'\uff3a') or (uchar >= u'\uff41' and uchar <= u'\uff5a'): return True else: return False def Q2B(uchar): """单个字符 全角转半角""" inside_code = ord(uchar) if inside_code == 0x3000: inside_code = 0x0020 else: inside_code -= 0xfee0 if inside_code < 0x0020 or inside_code > 0x7e: #转完之后不是半角字符返回原来的字符 return uchar return chr(inside_code) def stringQ2B(ustring): """把字符串全角转半角""" return "".join([Q2B(uchar) for uchar in ustring]) def stringpartQ2B(ustring): """把字符串中数字和字母全角转半角""" return "".join([Q2B(uchar) if is_Qnumber(uchar) or is_Qalphabet(uchar) else uchar for uchar in ustring]) def delete_newline_char(message): message= stringpartQ2B(message) message=message.replace('\r\n',' ') #删除message中的所有'\r\n' message = message.replace('\2', '') # 删除单词中间的换行连字符 message = stringQ2B(message) ch1="0" text="" for ch in message : if ch ==" " and ch1 >= '\u4e00' and ch1 <='\u9fa5': pass else: text +=ch ch1=ch print(text) return text content = pyperclip.paste() # 把剪贴板的内容粘贴到变量content中 content = delete_newline_char(content) # 删除content中的换行符 pyperclip.copy(content) # 把content的内容复制到剪贴板 while True: # 每隔一秒查询一次剪贴板,如果剪贴板的内容有更新,则重复刚才的动作 time.sleep(1) # 暂停一秒钟 content_tmp = pyperclip.paste() # 获取剪贴板的内容 if content_tmp != content: # 如果剪贴板的内容有更新,重复刚才的动作 content = content_tmp content = delete_newline_char(content) pyperclip.copy(content)
标签:字符,return,换行,python,ustring,uchar,pdf,message,def From: https://www.cnblogs.com/suoyike1001/p/16912935.html