当使用XLSXWriter导出到Excel时,如何更改URL字段的格式?

发布于 2025-02-01 15:12:46 字数 982 浏览 3 评论 0原文

我尝试更改从保存熊猫到下面的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生来就爱笑 2025-02-08 15:12:46

我真的看不到问题,但是可以肯定的是,建议您不声明变量writer = pd.excelwriter而不是使用whit

示例代码

import pandas as pd

tops = [
    {"name": "google", "url": "https://www.google.com"},
    {"name": "amazon", "url": "https://www.amazon.com"},
    {"name": "apple", "url": "https://www.apple.com"},
]

df = pd.DataFrame(tops)

with pd.ExcelWriter("excel.xlsx", engine="xlsxwriter") as writer:
    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)

print("save")

<强>响应

”在此处输入图像描述”

它起作用了!

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 use whit

example code

import pandas as pd

tops = [
    {"name": "google", "url": "https://www.google.com"},
    {"name": "amazon", "url": "https://www.amazon.com"},
    {"name": "apple", "url": "https://www.apple.com"},
]

df = pd.DataFrame(tops)

with pd.ExcelWriter("excel.xlsx", engine="xlsxwriter") as writer:
    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)

print("save")

response

enter image description here

this it worked!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文