python - How to output results from a conditional statement to a MS Word file in Python?
问题描述
I have also been updating a Word file using the Python doc-x library and building tables etc into this file. Now I would like to add the results of the IF function.
if any(df.loc[df.loc[:,'Column A'] =='Y']):
print (df.loc[:,'Value'] + "\n Placeholder A \nPlaceholder B\n" + str(table))
else:
print (df.loc[:,'Value'] + "\n Placeholder A \nPlaceholder B \n")
Any ideas on how I can achieve this? Currently, it is only printing the desired result to the console.
I have already tried using
doc.add_paragraph()
But there is no way to add in the conditional statement through this.
解决方案
你可以把python if
/else
内联:
new_value = df.loc[:, 'Value'] + "\n Placeholder A \nPlaceholder B\n" + (str(table) if any(df.loc[df.loc[:, 'Column A'] == 'Y']) else '')
''
所以 if/else 部分给出了一个要添加到末尾的空字符串,或者str(table)
接下来,只需添加new_value
到您的文档中。
推荐阅读
- reporting-services - SSRS多行明细部分不同列宽
- xslt - XSLT 1.0 - 使用子上下文的重复表不起作用
- python - 过拟合与欠拟合:训练集和测试集的平行线
- excel - Azure Blob 存储:从 Excel 工作簿中删除密码
- google-app-engine - App Engine 的“内部流量”是什么意思?
- java - 在读取文本文件的某些部分之前,让 Java Netbeans GUI 构建器在事件之前等待
- google-apps-script - 谷歌语音自动回复编码非工作时间
- oracle - PLS-00330:类型名称或子类型名称的使用无效
- php - 如何使用 implode 和 array_splice 结束句子?
- ios - 自 iOS 15 起不再出现 Xamarin 应用程序跟踪透明度对话框