Xlsxwriter insert_image URL 选项没有本地超链接
Xlsxwriter insert_image URL option not hyperlinking locally
我正在从 URL 下载图像,将它们保存在本地,然后将它们插入到 excel 文件中。我已经能够使用方法选项成功地将网站 URL 添加到图像中。我还能够手动将图像超链接到实际的 jpg 文件,因此默认图片查看器打开原始图像,未调整大小。
尽管如此,我无法完成使用库的最后一步。这是过程:
# Paths
image_file_path = os.path.join(image_folder_name, image_name)
image_absolute_path = os.path.join(os.getcwd(), image_file_path)
print(image_file_path)
print(image_absolute_path)
download_image(full_url, image_file_path)
# Save as JPG format to remove constraints
image_size = save_image(image_file_path, width, height)
resize_scale = get_resized_scales(image_size, (width, height))
options = {'x_scale': resize_scale[0],
'y_scale': resize_scale[1],
'x_offset': 7,
'y_offset': 7,
'object_position': 1,
'url': image_absolute_path
}
worksheet.insert_image(row_index, 4, image_file_path, options)
打印两个路径输出:
images\image.jpg
C:\Users\Me\IdeaProjects\Downloader\images\image.jpg
我在 'url' 部分都试过了,就是超链接不存在。
Python 版本 3.8.0
xlxswriter 1.2.6
Windows 10 OS
XlsxWriter 中的本地超链接需要以 'external:'
为前缀(有关 XlxsWriter URL 处理的完整说明,请参阅 write_url() 文档)。
所以在你的情况下,以下应该有效:
options = {'x_scale': resize_scale[0],
'y_scale': resize_scale[1],
'x_offset': 7,
'y_offset': 7,
'object_position': 1,
'url': 'external:' + image_absolute_path}
我正在从 URL 下载图像,将它们保存在本地,然后将它们插入到 excel 文件中。我已经能够使用方法选项成功地将网站 URL 添加到图像中。我还能够手动将图像超链接到实际的 jpg 文件,因此默认图片查看器打开原始图像,未调整大小。
尽管如此,我无法完成使用库的最后一步。这是过程:
# Paths
image_file_path = os.path.join(image_folder_name, image_name)
image_absolute_path = os.path.join(os.getcwd(), image_file_path)
print(image_file_path)
print(image_absolute_path)
download_image(full_url, image_file_path)
# Save as JPG format to remove constraints
image_size = save_image(image_file_path, width, height)
resize_scale = get_resized_scales(image_size, (width, height))
options = {'x_scale': resize_scale[0],
'y_scale': resize_scale[1],
'x_offset': 7,
'y_offset': 7,
'object_position': 1,
'url': image_absolute_path
}
worksheet.insert_image(row_index, 4, image_file_path, options)
打印两个路径输出:
images\image.jpg
C:\Users\Me\IdeaProjects\Downloader\images\image.jpg
我在 'url' 部分都试过了,就是超链接不存在。
Python 版本 3.8.0
xlxswriter 1.2.6
Windows 10 OS
XlsxWriter 中的本地超链接需要以 'external:'
为前缀(有关 XlxsWriter URL 处理的完整说明,请参阅 write_url() 文档)。
所以在你的情况下,以下应该有效:
options = {'x_scale': resize_scale[0],
'y_scale': resize_scale[1],
'x_offset': 7,
'y_offset': 7,
'object_position': 1,
'url': 'external:' + image_absolute_path}