当使用XLSXWriter导出到Excel时,如何更改URL字段的格式?
我尝试更改从保存熊猫到下面的excel代码所获得的URL字段的格式,
应该适用于Excel文件的所有可用区域,但由于某种原因,第一列包含Web Adresses并没有改变其样式。取而代之的是,它仍然使用默认的excel格式化
writer = pd.ExcelWriter(f"{date_filename}.xlsx", engine="xlsxwriter")
df.to_excel(writer, index=False, sheet_name='current')
workbook = writer.book
worksheet = writer.sheets['current']
font_format = workbook.add_format({'font_name': 'Calibri', 'font_size': 13})
header_format = workbook.add_format({'font_name': 'Calibri', 'font_size': 13, 'bold': True})
worksheet.set_column('A:H', None, font_format)
worksheet.set_row(0, None, header_format)
writer.save()
我的df具有几列,其中第一个是一个URL One(一个对象dtype)
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 url 2 non-null object
,我在其中存储WG,
https://google.com in a first row,
https://amazon.com in a second etc
我想知道为什么它可以保持默认的Excel格式化。
I try to change the formatting of the URL fields which I got from saving pandas to excel
Code below should work for all usable area of my excel file but for some reason first column which contain web adresses didn't changed its style. Instead it still use the default excel formatting
writer = pd.ExcelWriter(f"{date_filename}.xlsx", engine="xlsxwriter")
df.to_excel(writer, index=False, sheet_name='current')
workbook = writer.book
worksheet = writer.sheets['current']
font_format = workbook.add_format({'font_name': 'Calibri', 'font_size': 13})
header_format = workbook.add_format({'font_name': 'Calibri', 'font_size': 13, 'bold': True})
worksheet.set_column('A:H', None, font_format)
worksheet.set_row(0, None, header_format)
writer.save()
My df have a few columns where the first one is a url one (an object Dtype)
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 url 2 non-null object
Where I store w.g.
https://google.com in a first row,
https://amazon.com in a second etc
I am wondering why it keeps the default excel formatting.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我真的看不到问题,但是可以肯定的是,建议您不声明变量
writer = pd.excelwriter
而不是使用whit
示例代码
<强>响应
它起作用了!
i really don't see the problem, but to be sure, it is recommended that you don't declare the variable
writer = pd.ExcelWriter
instead usewhit
example code
response
this it worked!