【问题标题】:How can turn <Cell 'Sheet'.A2> to only 2?如何将 <Cell 'Sheet'.A2> 变成只有 2?
【发布时间】:2022-01-18 22:11:29
【问题描述】:

我对 openpyxl 有疑问。我的代码正在运行,但我不知道如何将价值保存到 excel

我的代码是

def get_title(link):
    global list2
    driver = create_driver()
    driver.get(link)
    source = BeautifulSoup(driver.page_source, "lxml")
    title = source.select_one("title").text
    print(f"{link}: '{title}'")
    
    for row in ws.rows:
        if row[0].value == link:
            print(row)
            # ws.cell(row=???, column=2).value = title
            # wb.save("site.xlsx")

然后打印出来

https://www.google.com/search?q=2&oq=2&aqs=chrome..69i57j69i59l2j0i271l2j69i60l2j69i61.623j0j1&sourceid=chrome&ie=UTF-8: '2 - Google search'
(<Cell 'Sheet'.A2>, <Cell 'Sheet'.B2>)
https://www.google.com/search?q=6&oq=6&aqs=chrome..69i57j0i271l3j69i60l3.3687j0j1&sourceid=chrome&ie=UTF-8: '6 - Google search'
(<Cell 'Sheet'.A1>, <Cell 'Sheet'.B1>)

我想做的是我想将标题保存到 excel 中的 B 列,但我不知道该怎么做

【问题讨论】:

  • 这有点老了,但它应该能让你在使用 pandas 库的帮助下取得进步:stackoverflow.com/questions/49681392/…
  • 谢谢,但它对我不起作用,我需要在 excel 中匹配 url 和标题。如果我转向熊猫,我需要更改许多代码
  • 不仅仅是row[1].value = title ?

标签: python python-3.x excel openpyxl


【解决方案1】:
            for row in ws.iter_rows(0):
                for cell in row:
                    if cell.value == "test":
                        ws.cell(row=cell.row, column=2).value = "test"
                        wb.save("test.xlsx")

这个解决了我的问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多