python - Docx-Python 更改文本不透明度
问题描述
我正在替换 docx 文件中的文本,该文件最初的文本不透明度为 25%,但是当我执行脚本时,已更改段落的不透明度恢复为 0%。有没有办法在不透明度中进行编程,类似于字体大小、斜体等?我在 docx-python 文档中没有找到任何内容。
编辑:这是代码的 docx 部分。
styles = d.styles
style = styles.add_style('Main Text', WD_STYLE_TYPE.PARAGRAPH)
mainStyle = d.styles['Main Text']
fontMain = mainStyle.font
fontMain.name = 'Montserrat Light'
fontMain.size = Pt(12)
styles = d.styles
style = styles.add_style('Main Run Text', WD_STYLE_TYPE.CHARACTER)
mainRunStyle = d.styles['Main Run Text']
fontRunMain = mainRunStyle.font
fontRunMain.name = 'Montserrat Light'
fontRunMain.size = Pt(12)
currentDate = datetime.date.today()
currentDateFormatted = currentDate.strftime("%B %d, %Y")
d.paragraphs[10].text = currentDateFormatted
d.paragraphs[12].text = 'Jimmy Newtron' #replace with argument
pName = d.paragraphs[12]
pName.style = d.styles['Main Text']
d.paragraphs[13].text = 'Lead Recruiter' #replace with argument
pTitle = d.paragraphs[13]
pTitle.style = d.styles['Main Text']
d.paragraphs[14].text = 'Mega Corp' #replace with argument
pCompany = d.paragraphs[14]
pCompany.style = d.styles['Main Text']
d.paragraphs[15].text = 'New York City, New York, United States' #replace with argument
pLocation = d.paragraphs[15]
pLocation.style = d.styles['Main Text']
d.paragraphs[17].runs[2].text = 'Jimmy Newtron' #replace with argument
rDear = d.paragraphs[17].runs[2]
rDear.style = d.styles['Main Run Text']
d.paragraphs[18].runs[2].text = 'Mega Corp' #replace with argument
rCompany = d.paragraphs[18].runs[2]
rCompany.style = d.styles['Main Run Text']
d.paragraphs[21].runs[3].text = 'Mega Corp' #replace with argument
rCompanyTwo = d.paragraphs[21].runs[3]
rCompanyTwo = d.styles['Main Run Text']
d.save('c:\\users\\sonorityscape\\desktop\\test.docx')
解决方案
我找到的一个解决方案是将文本保存为运行而不是段落。出于某种原因,它保留了您保存在原始文档中的透明度/不透明度。
推荐阅读
- outlook - 程序化日历邀请未添加到 Office 365 的组织者日历中
- cypher - 用于 gremlin 遍历的 Cypher 查询
- python - 如何在带有熊猫的列数据框前面添加值
- python - 如何显示布尔字段为真的 django 模型的数据?
- java - Hazelcast 中的集群合并通知
- python - selenium + Python csv 文件输出搞砸了
- r - 如何让 R shiny 读取 .xlsx 和 csv 文件而不会发生冲突?
- javascript - VScode 扩展中的“onDidChangeTextDocument()”承诺是否依赖于用户的活动窗口开始收听?
- python - 在 captum 中调用模型层的名称时出现语法错误
- javascript - javascript从2个数组创建数据对象