英文:
Exporting list to Excel and creation of hyperlink - error because of tuple data
问题
我有一个包含元组的列表,我想使用Python将其导出到Excel。该列表的结构如下所示:
[('ABC-12345', 'hello', 'world', 'https://mycompany.com/browse/ABC-12345'), ..., ('ABC-12360', 'good', 'night', 'https://mycompany.com/browse/ABC-12360')]
此外,列表的每一行的第一个元素,即'ABC-12345',应转换为超链接。链接的URL是第四个元素,即'https://mycompany.com/browse/ABC-12345'。为了解决这个问题,我尝试使用openpyxl中的Workbook和for循环,其中我将第一列重写为第四个元素作为超链接。这引发了一个元组错误,因为for循环需要n是一个整数或布尔值。
我从以下代码开始:
from openpyxl import Workbook
search = [('ABC-12345', 'hello', 'world', 'https://mycompany.com/browse/ABC-12345'),
...,
('ABC-12360', 'good', 'night', 'https://mycompany.com/browse/ABC-12360')]
wb = Workbook()
ws = wb.active
search_row = 1
search_column = 1
for n in search:
row = search[n]
ws.cell(row=n, column=n).value = row[0]
ws.cell(row=n, column=n).hyperlink = row[3] # Overwriting cell n to create hyperlink
n = n + 1
wb.save("testReport.xlsx")
为了避免元组错误,我尝试写成'for n in enumerate(search):',但没有成功。因此,我改变了我的方法,尝试使用:
import numpy as np
import csv
import pyexcel
from pyexcel._compact import OrderedDict
search = [('ABC-12345', 'hello', 'world', 'https://mycompany.com/browse/ABC-12345'),
...,
('ABC-12360', 'good', 'night', 'https://mycompany.com/browse/ABC-12360')]
# 将元组列表转换为数组
arr = np.asarray(search)
# 将数组添加到字典中
dictionary = {'Sheet 1': arr}
pyexcel.save_book_as(
bookdict = dictionary,
dest_file_name = "testReport.xlsx"
)
data = OrderedDict()
data.update({"Sheet 1": dictionary['Sheet 1']})
这确实为我生成了一个Excel文件,但是.xls似乎是一种过时的文件类型,每次运行程序时我都需要删除现有文件,因为文件名已在使用中。在编辑xls文件方面似乎自由度很小,我没有找到创建超链接的方法。接下来,由于在这些库中找不到解决方法,我想也许我可以将这种方法与我的第一种方法结合起来。这要求我将文件保存为.xlsx,并通过这样做我又回到了元组问题。
你有解决这个问题的想法吗?感谢您的建议。
英文:
I have a list consisting of tuples that I want to export to excel using python. The list is structured in the following way:
[('ABC-12345', 'hello', 'world', 'https://mycompany.com/browse/ABC-12345'), ..., ('ABC-12360', 'good', 'night', 'https://mycompany.com/browse/ABC-12360')]
On top of this, should the first element in each list row, i.e. ''ABC-12345'', be converted to a hyperlink. The URL for the link is the fourth element, i.e. ''https://mycompany.com/browse/ABC-12345''. To solve this I tried to use Workbook from openpyxl and a for-loop, where i overwrite the first column with the fourth element as a hyperlink. This raises a tuple-error, since the for-loop need n to be an integer or boolean.
I started of with the code:
from openpyxl import Workbook
search = [('ABC-12345', 'hello', 'world', 'https://mycompany.com/browse/ABC-12345'),
...,
('ABC-12360', 'good', 'night', 'https://mycompany.com/browse/ABC-12360')]
wb = Workbook()
ws = wb.active
search_row = 1
search_column = 1
for n in search:
row = search[n]
ws.cell(row=n, column=n).value = row[0]
ws.cell(row=n, column=n).hyperlink = row[3] # Overwriting cell n to create hyperlink
n = n + 1
wb.save("testReport.xlsx")
To get around the tuple-error I tried to write ''for n in enumerate(search):'' instead, but without luck. Hence, I changed my approach and tried using:
import numpy as np
import csv
import pyexcel
from pyexcel._compact import OrderedDict
search = [('ABC-12345', 'hello', 'world', 'https://mycompany.com/browse/ABC-12345'),
...,
('ABC-12360', 'good', 'night', 'https://mycompany.com/browse/ABC-12360')]
# Converting list of tuple to array
arr = np.asarray(search)
# Adding array to a dictionary
dictionary = {'Sheet 1': arr}
pyexcel.save_book_as(
bookdict = dictionary,
dest_file_name = "testReport.xlsx"
)
data = OrderedDict()
data.update({"Sheet 1": dictionary['Sheet 1']})
This does give me an excel-file, but .xls seems to be an outdated file-type and every time I run the program I need to delete the existing file since the filename is already in use. There also seems to be little freedom with editing the xls-file - I didn't find a way to create the hyperlink. Moving on, since I didn't find a solution in these libraries, I thought that maybe I could combine this method with my first method. That required me to save the file as .xlsx, and by doing so I was brought back to the tuple-problem.
Any ideas on how I can solve this? Thankful for suggestions.
答案1
得分: 0
我猜你想要类似这样的东西:
for n, row in enumerate(search, start=1):
ws.cell(row=n, column=1).value = row[0]
ws.cell(row=n, column=1).hyperlink = row[3] # 覆盖单元格 n 以创建超链接
wb.save("testReport.xlsx")
英文:
I suppose you want something like:
for n, row in enumerate(search, start=1):
ws.cell(row=n, column=1).value = row[0]
ws.cell(row=n, column=1).hyperlink = row[3] # Overwriting cell n to create hyperlink
wb.save("testReport.xlsx")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论