擅长:python、mysql、java
<p>或者更好的方法是<code>set_properties</code>:</p>
<pre><code>with pd.ExcelWriter("test.xlsx") as writer:
df.style.set_properties(**{'text-align': 'center'}).to_excel(
writer,
index=False,
header=False,
)
</code></pre>
<p>这会将所有文本对齐到中心</p>
<p>这将起作用,因为它将属性<code>text-align</code>设置为<code>center</code>。然后使用上下文管理器实例(<code>with</code>语句),它可以写入excel文件</p>