记录一个报错问题 关于调用win32com打开文档
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)
记录一个报错问题 关于调用win32com打开文档相关推荐
- iOS/iPhone 微信打开doc xls文件报错OfficeImportErrorDomain错误912 打不开文档
如图所示. 解决方案: 点击右上角的三个点,选择用其他应用打开 观察红圈处显示的图标,是哪个第三方APP,卸载掉!!!!直到变成如图所示的Word图标! 问题解决
- 调用http时因为js问题遇到的一个报错
调用http时因为js问题遇到的一个报错 如果在使用其他语言调用http进行读取页面数据.获取页面源代码.接口调试等等时遇到了以下的报错: <html> <body> < ...
- 解决使用mybatis分页插件PageHelper的一个报错问题
解决使用mybatis分页插件PageHelper的一个报错问题 参考文章: (1)解决使用mybatis分页插件PageHelper的一个报错问题 (2)https://www.cnblogs.co ...
- php fpm 日志记录,如何解决nginx下php-fpm不记录php报错日志的问题
如何解决nginx下php-fpm不记录php报错日志的问题 发布时间:2020-07-28 10:17:29 来源:亿速云 阅读:150 作者:Leah 本篇文章为大家展示了如何解决nginx下ph ...
- nginx php fpm 日志,nginx下php-fpm不记录php报错日志怎么办?
解决nginx下php-fpm不记录php报错日志的办法:1.修改[php-fpm.conf]中配置,没有则增加:2.修改[php.ini]中配置,没有则增加:3.重启[php-fpm]即可. 解决n ...
- eas报错日记_EAS8.1预留记录查询报错
EAS8.1预留记录查询报错,点击忽略后,可进入预留记录界面. 报错信息如下: 错误信息:null 错误详细堆栈信息:java.lang.NullPointerException: null at: ...
- 解决typescript报错:不能调用可能是未定义的对象
背景:当我们在react中封装组件并复用时,会传不同的props,那么自然有些props是不需要传的,所以在定义interface的时候需要加个问号.但这也就引来了标题的报错! 模拟一下报错代码: / ...
- logstash mysql eror_Logstash安装记录(报错com.mysql.jdbc.Driver not loaded的解决)
Logstash是es官方标配的ELK中的"L",它们三者的关系是这样:"E"指elasticsearch,用来提供核心的搜索服务:"L"指 ...
- mysql scope runtime_Maven依赖scope属性详解-一个报错引发的问题 - 老郭种树
ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the cont ...
- (转载)如何跟踪一个报错消息
在SAP项目中,经常会遇到一个比较复杂的程序报出个错误消息(有时会是一个标准SAP程序报错),但是不知道到底是哪段ABAP代码出错了. 下面通过一个简单的例子,来介绍6种方法来定位报message的A ...
最新文章
- 共轭梯度下降法matlab,用matlab实现最速下降法,牛顿法和共轭梯度法求解实例
- 去除C++中string前面和后面的空白符
- Corosync+Pacemaker+DRBD实现Mysql服务的高可用
- adaboost算法java代码_04-04 AdaBoost算法代码(鸢尾花分类)
- ISBN号码(洛谷-P1055)
- shell 列表_Shell文本编辑之转录因子(TF)列表的获取
- Software--Spring Boot--Contact 项目初期
- 跟据URL来实现WebService
- 说明assert()的作用:assert()是一个调试程序时经常使用的宏,在程序执行时计算括号内的表达式,如果表达式为假,程序报告错误,并终止执行;若表达式为真,则继续执行后面的语句。
- linux下shell脚本编程2
- 戏人看戏,苏旭博客网-学无止尽
- 从傅里叶级数到傅里叶变换
- Java验证辛钦大数定理
- 数据结构与算法之链表结构寻找p、q最近的公共祖先
- 资源管理器整理计算机中的文件,如何整理电脑文件夹?
- c#设计一个Windows应用程序,模拟一个简单的银行账户管理系统。完成“创建账A款”“存款”和“查询余额”的模拟操作。程序功能如下:
- matlab中nargin函数
- Markdown表格、单元格合并、快速编辑表格
- 搞“两弹一星”也离不开计算尺
- ​基于机器视觉的视觉刺激BCI系统实现动态控制