error :
module 'win32com.gen_py.00020905-0000-4B30-A977-D214852036FFx0x3x0' has no attribute 'CLSIDToClassMap
解决办法:删除目录C:\Users\%username%\AppData\Local\Temp\gen_py\3.7 中的缓存文件夹00020905-0000-4B30-A977-D214852036FFx0x3x0即可,重新执行上述代码便不再报错.
doc_app = win32.gencache.EnsureDispatch('Word.Application')
doc_app.Visible = False
,,,,,
def yibden2zh_word(): #bakdu
import win32com.client as win32
from win32com.client import constants
import os
global content2file
content = t1yibdleftxlsxframe.get(0.0, 'end')
content2file = content.replace('\n', '')
outffname = content2file.split('.')[-2]
from_lang = 'en'
to_lang = 'zh'
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=8al6rc2kFiDwgK6&client_secret=A4huAdpSW4LFVI0aL8ECothiYpGdk7'
response_token = requests.get(host)
print(response_token.json())
print(type(response_token.json()))
token = response_token.json()['access_token']
print(token)
url = 'https://aip.baidubce.com/rpc/2.0/mt/texttrans/v1?access_token=' + token
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
def make_md5(s, encoding='utf-8'):
return md5(s.encode(encoding)).hexdigest()
wordfile = docx.Document(content2file)
wordfile_new = docx.Document()
sections = wordfile.sections
sec = sections[0]
sections_new = wordfile_new.sections
sec_new = sections_new[0]
sec_new.left_margin = sec.left_margin
sec_new.right_margin = sec.right_margin
sec_new.top_margin = sec.top_margin
sec_new.bottom_margin = sec.bottom_margin
sec_new.header_distance = sec.header_distance
sec_new.orientation = sec.orientation
sec_new.page_height = sec.page_height
sec_new.page_width = sec.page_width
for paragraph in wordfile.paragraphs:
if not paragraph.text:
wordfile_new.add_paragraph('\n')
print("not paragraph.text")
continue
# print("continue")
size_lst = []
bold_lst = []
italic_lst = []
color_lst = []
highlight_color_lst = []
underline_lst = []
strike_lst = []
double_strike_lst = []
subscript_lst = []
superscript_lst = []
for run in paragraph.runs:
# if run.text:
# runt.append(run.text)
# runtext=''
# paragraph.add_run(''.join(runt))
size_lst.append(run.font.size)
bold_lst.append(run.font.bold)
italic_lst.append(run.font.italic)
color_lst.append(run.font.color.rgb)
highlight_color_lst.append(run.font.highlight_color)
underline_lst.append(run.font.underline)
strike_lst.append(run.font.strike)
double_strike_lst.append(run.font.double_strike)
subscript_lst.append(run.font.subscript)
superscript_lst.append(run.font.superscript)
p = wordfile_new.add_paragraph()
p.paragraph_format.alignment = paragraph.paragraph_format.alignment
p.paragraph_format.left_indent = paragraph.paragraph_format.left_indent
p.paragraph_format.right_indent = paragraph.paragraph_format.right_indent
p.paragraph_format.first_line_indent = paragraph.paragraph_format.first_line_indent
p.paragraph_format.line_spacing = paragraph.paragraph_format.line_spacing
p.paragraph_format.space_before = paragraph.paragraph_format.space_before
p.paragraph_format.space_after = paragraph.paragraph_format.space_after
query = paragraph.text
print(query)
if query == '\n':
continue
payload = {'q': query, 'from': from_lang, 'to': to_lang, }
print(payload)
r = requests.post(url, params=payload, headers=headers)
result = r.json()
print(result)
for res in result['result']['trans_result']:
p.add_run(res['dst'])
for run in p.runs:
run.font.name = '微软雅黑'
r = run._element.rPr.rFonts
r.set(qn('w:eastAsia'), '微软雅黑')
run.font.size = max(size_lst, key=size_lst.count)
run.font.bold = max(bold_lst, key=bold_lst.count)
run.font.italic = max(italic_lst, key=italic_lst.count)
run.font.color.rgb = max(color_lst, key=color_lst.count)
run.font.highlight_color = max(highlight_color_lst, key=highlight_color_lst.count)
run.font.underline = max(underline_lst, key=underline_lst.count)
run.font.strike = max(strike_lst, key=strike_lst.count)
run.font.subscript = max(subscript_lst, key=subscript_lst.count)
run.font.superscript = max(superscript_lst, key=superscript_lst.count)
outfilename_bd = asksaveasfilename(initialfile='%s-翻译后.docx' % outffname, defaultextension='.docx')
wordfile_new.save(outfilename_bd)
#wordfile_new.close()
time.sleep(2)
from win32com.client import Dispatch, DispatchEx
import win32com
import win32com.client
# get pic from old file
#doc_app = win32.gencache.EnsureDispatch('Word.Application') # 打开word应用程序
doc_app = win32.gencache.EnsureDispatch('Word.Application')
doc_app.Visible = False
ooldfile_path = content2file
doc = doc_app.Documents.Open(ooldfile_path)
# 复制word的所有内容
doc.Content.Copy()
doc.Close()
word = win32com.client.DispatchEx('Word.Application')
doc1 = word.Documents.Open(outfilename_bd)
s = word.Selection
s.MoveRight(1, doc1.Content.End) # 将光标移动到文末
s.Paste()
doc1.Close()
os.startfile(outfilename_bd)