欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python使用openpyxl實現對excel表格相對路徑的超鏈接的創(chuàng)建方式

 更新時間:2024年03月05日 14:09:13   作者:假期的學習  
這篇文章主要介紹了python使用openpyxl實現對excel表格相對路徑的超鏈接的創(chuàng)建方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

使用openpyxl實現對excel表格相對路徑的超鏈接的創(chuàng)建

 # 這個是相對路徑,可以修改父文件夾(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實現操縱mysql數據庫插入

    用python實現操縱mysql數據庫插入

    大家好,本篇文章主要講的是用python實現操縱mysql數據庫插入,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-01-01
  • Tensorflow實現部分參數梯度更新操作

    Tensorflow實現部分參數梯度更新操作

    今天小編就為大家分享一篇Tensorflow實現部分參數梯度更新操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01
  • python字符串拼接.join()和拆分.split()詳解

    python字符串拼接.join()和拆分.split()詳解

    這篇文章主要為大家介紹了python字符串拼接.join()和拆分.split(),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-11-11
  • python實現簡單井字棋小游戲

    python實現簡單井字棋小游戲

    這篇文章主要為大家詳細介紹了python實現簡單井字棋小游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • Python中else怎么用?else的用法總結

    Python中else怎么用?else的用法總結

    這篇文章主要介紹了Python中else的用法總結,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • 利用Python通過獲取剪切板數據實現百度劃詞搜索功能

    利用Python通過獲取剪切板數據實現百度劃詞搜索功能

    大家是不是嫌棄每次打開百度太麻煩?今天教大家利用Python通過獲取剪切板數據實現百度劃詞搜索功能,用程序直接打開網頁,需要的朋友可以參考下
    2021-06-06
  • python繪圖時,坐標軸負號顯示不出來的解決

    python繪圖時,坐標軸負號顯示不出來的解決

    這篇文章主要介紹了python繪圖時,坐標軸負號顯示不出來的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • python實現nao機器人手臂動作控制

    python實現nao機器人手臂動作控制

    這篇文章主要為大家詳細介紹了python實現nao機器人手臂動作控制,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • python使用phoenixdb操作hbase的方法示例

    python使用phoenixdb操作hbase的方法示例

    這篇文章主要介紹了python使用phoenixdb操作hbase的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-02-02
  • python中?OpenCV和Pillow處理圖像操作及時間對比

    python中?OpenCV和Pillow處理圖像操作及時間對比

    這篇文章主要介紹了python中OpenCV和Pillow處理圖像操作及時間對比,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-09-09

最新評論