python使用openpyxl实现对excel表格相对路径的超链接的创建方式
作者:假期的学习
这篇文章主要介绍了python使用openpyxl实现对excel表格相对路径的超链接的创建方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
使用openpyxl实现对excel表格相对路径的超链接的创建
# 这个是相对路径,可以修改父文件夹(images这个文件夹名不能更改) # img_path: images路径下的图片名 sheet.cell(row=img_site1_2 + 1, column=img_site2_2).hyperlink = 'images\\' + img_path
其他相关代码
# 设置居中靠底部显示
align2 = Alignment(horizontal='right', vertical='justify')
try:
# 使用tqdm库进行进度条显示
with tqdm(iterable=images, desc='图片导入', unit='张', total=len(images)) as pgbr:
for img_path in images:
# 处理图片名称(获取插入地址)
img_sites = img_path.split('_')
# 行数(需要+1)
img_site1 = int(img_sites[0])
img_site1_2 = img_site1
# sheet.row_dimensions[img_site1+1].height=40
img_site1 = str(img_site1+1)
# 转换为int,方便使用这个值当数组下标去取clos的值,列数
img_site2 = int(img_sites[1])
# if img_site2 == 0:
# img_site2 = int(img_sites[2])
img_site2_2 = img_site2 + 11
# 数组取值从0开始,而获取到的值是从1开始,真正的列值
img_site2 = cols[img_site2 - 1]
# 图片真正的地址
img_path_real = savepath2 + '\\' + img_path
# 插入图片本地地址
# 这个是绝对路径(换一台电脑就不好使了)
# file_name = 'file:///' + img_path_real # 这个是多余的,链接生成自动会产生
# 使用相对路径,换一台电脑也可以正常访问图片)
sheet.column_dimensions[img_site2].width=18
# sh = sheet.row_dimensions[img_site1_2+1].height
# print("sh: ",sh)
# 插入到excel中的位置
position = img_site2 + img_site1
# 这个是绝对路径,修改文件夹名称就不能用了
# sheet.cell(row=img_site1_2 + 1, column=img_site2_2).hyperlink = img_path_real
# 这个是相对路径,可以修改父文件夹(images这个文件夹名不能更改)
sheet.cell(row=img_site1_2 + 1, column=img_site2_2).hyperlink = 'images\\' + img_path
sheet.cell(row=img_site1_2 + 1, column=img_site2_2).style = "Hyperlink"
sheet.cell(row=img_site1_2 + 1, column=img_site2_2).value = '详情'
sheet.cell(row=img_site1_2 + 1, column=img_site2_2).alignment = align2
# 可能存在插入的是视频(视频就直接以文件形式插入)
try:
img = Image(img_path_real)
img.width = 50
img.height = 50
# 插入图片
sheet.add_image(img, position)
except Exception as e:
sheet.cell(row=img_site1_2 + 1, column=img_site2_2).value = '视频' + str(img_site1_2) + '_' + str(img_site2_2 - 11)
sheet.cell(row=img_site1_2 + 1, column=img_site2_2).alignment = Alignment(horizontal='center', vertical='center')
# 进度条
pgbr.update(1)
pass
path_filename2 = savepath + '\\' + 'xxx年xx月_' + '.xlsx'
wb.save(path_filename2)
except Exception as e:
print(e)
return e
openpyxl超链接添加
openpyxl的cell有属性hyperlink属性
这个属性可以设置超链接,如果只是想设置一列有超链接,可以用if来设置
def write_to_execl_link(filename = './新建.xlsx',title='sheet',sheet_header = [],sheet_data = []):
import openpyxl
wb = openpyxl.Workbook()
ws = wb.active
ws.title = title
row0 = sheet_header
for i, r in enumerate(row0):
ws.cell(row=1, column=i + 1, value=r)
for i, r in enumerate(sheet_data):
for j, c in enumerate(r):
# if i%2 == 1:
# ws.cell(row=i + 2, column=j + 1).fill = fill
ws.cell(row=i + 2, column=j + 1, value=c)
ws.cell(row=i + 2, column=j + 1).hyperlink = r[-1]
wb.save(filename)openpyxl获取文件内容的超链接
import openpyxl import os #将Excel文件放在python同级目录 dir_path = os.path.dirname(os.path.realpath(__file__)) test_xlsx = os.path.join(dir_path,f'''test.xlsx''') wb = openpyxl.load_workbook(test_xlsx) sheet = wb.active print(sheet.cell(1, 1).value) print(sheet.cell(1, 1).hyperlink.target)
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
- Python使用pandas和openpyxl读取Excel表格的方法详解
- python使用openpyxl打开及读取excel表格过程
- python openpyxl提取Excel图片实现原理技巧
- python openpyxl操作Excel的安装使用
- python使用openpyxl库处理Excel文件详细教程
- Python通过OpenPyXL处理Excel的完整教程
- python使用openpyxl库读取Excel文件数据
- python如何通过openpyxl读写Excel文件
- Python Excel操作从零学习掌握openpyxl用法
- 使用python中的openpyxl操作excel详解
- python操作Excel神器openpyxl看这一篇就够了
- python使用Openpyxl操作Excel文件的实现
