最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

使用openpyxl實(shí)現(xiàn)對(duì)excel表格相對(duì)路徑的超鏈接的創(chuàng)建

 # 這個(gè)是相對(duì)路徑,可以修改父文件夾(images這個(gè)文件夾名不能更改)
 # img_path: images路徑下的圖片名
sheet.cell(row=img_site1_2 + 1, column=img_site2_2).hyperlink = 'images\\' + img_path

其他相關(guān)代碼

# 設(shè)置居中靠底部顯示
    align2 = Alignment(horizontal='right', vertical='justify')
    try:
    	# 使用tqdm庫(kù)進(jìn)行進(jìn)度條顯示
        with tqdm(iterable=images, desc='圖片導(dǎo)入', unit='張', total=len(images)) as pgbr:
            for img_path in images:
                # 處理圖片名稱(獲取插入地址)
                img_sites = img_path.split('_')
                # 行數(shù)(需要+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)
                # 轉(zhuǎn)換為int,方便使用這個(gè)值當(dāng)數(shù)組下標(biāo)去取clos的值,列數(shù)
                img_site2 = int(img_sites[1])
                # if img_site2 == 0:
                #     img_site2 = int(img_sites[2])
                img_site2_2 = img_site2 + 11
                # 數(shù)組取值從0開始,而獲取到的值是從1開始,真正的列值
                img_site2 = cols[img_site2 - 1]
                # 圖片真正的地址
                img_path_real = savepath2 + '\\' + img_path
                # 插入圖片本地地址
                # 這個(gè)是絕對(duì)路徑(換一臺(tái)電腦就不好使了)
                # file_name = 'file:///' + img_path_real # 這個(gè)是多余的,鏈接生成自動(dòng)會(huì)產(chǎn)生
                # 使用相對(duì)路徑,換一臺(tái)電腦也可以正常訪問(wèn)圖片)
                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
                # 這個(gè)是絕對(duì)路徑,修改文件夾名稱就不能用了
                # sheet.cell(row=img_site1_2 + 1, column=img_site2_2).hyperlink = img_path_real 
                # 這個(gè)是相對(duì)路徑,可以修改父文件夾(images這個(gè)文件夾名不能更改)
                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')
                # 進(jìn)度條
                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屬性

這個(gè)屬性可以設(shè)置超鏈接,如果只是想設(shè)置一列有超鏈接,可以用if來(lái)設(shè)置

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獲取文件內(nèi)容的超鏈接

import openpyxl
import os
#將Excel文件放在python同級(jí)目錄
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)

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 利用python對(duì)mysql表做全局模糊搜索并分頁(yè)實(shí)例

    利用python對(duì)mysql表做全局模糊搜索并分頁(yè)實(shí)例

    這篇文章主要介紹了利用python對(duì)mysql表做全局模糊搜索并分頁(yè)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-07-07
  • Python多線程 Queue 模塊常見(jiàn)用法

    Python多線程 Queue 模塊常見(jiàn)用法

    Python的Queue模塊提供一種適用于多線程編程的FIFO實(shí)現(xiàn)。它可用于在生產(chǎn)者(producer)和消費(fèi)者(consumer)之間線程安全(thread-safe)地傳遞消息或其它數(shù)據(jù),因此多個(gè)線程可以共用同一個(gè)Queue實(shí)例。Queue的大小(元素的個(gè)數(shù))可用來(lái)限制內(nèi)存的使用
    2021-07-07
  • 使用Pandas計(jì)算系統(tǒng)客戶名稱的相似度

    使用Pandas計(jì)算系統(tǒng)客戶名稱的相似度

    在日常業(yè)務(wù)處理中,我們經(jīng)常會(huì)面臨將不同系統(tǒng)中的數(shù)據(jù)進(jìn)行匹配和比對(duì)的情況,本文將介紹如何使用Python的Pandas庫(kù)來(lái)處理這個(gè)問(wèn)題,需要的可以參考一下
    2023-07-07
  • Python閉包實(shí)現(xiàn)計(jì)數(shù)器的方法

    Python閉包實(shí)現(xiàn)計(jì)數(shù)器的方法

    這篇文章主要介紹了Python閉包實(shí)現(xiàn)計(jì)數(shù)器的方法,分析了閉包的概念及實(shí)現(xiàn)計(jì)數(shù)器的相關(guān)技巧,需要的朋友可以參考下
    2015-05-05
  • Linux下python3.7.0安裝教程

    Linux下python3.7.0安裝教程

    這篇文章主要為大家詳細(xì)介紹了Linux下python3.7.0安裝教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • Python Request類源碼實(shí)現(xiàn)方法及原理解析

    Python Request類源碼實(shí)現(xiàn)方法及原理解析

    這篇文章主要介紹了Python Request類源碼實(shí)現(xiàn)方法及原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08
  • Python 查詢學(xué)生的平均分?jǐn)?shù)的實(shí)現(xiàn)

    Python 查詢學(xué)生的平均分?jǐn)?shù)的實(shí)現(xiàn)

    這篇文章主要為大家詳細(xì)介紹了Python 查詢學(xué)生的平均分?jǐn)?shù)的實(shí)現(xiàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • python使用Random隨機(jī)生成列表的方法實(shí)例

    python使用Random隨機(jī)生成列表的方法實(shí)例

    在日常的生活工作和系統(tǒng)游戲等設(shè)計(jì)和制作時(shí),經(jīng)常會(huì)碰到產(chǎn)生隨機(jī)數(shù),用來(lái)解決問(wèn)題,下面這篇文章主要給大家介紹了關(guān)于python使用Random隨機(jī)生成列表的相關(guān)資料,需要的朋友可以參考下
    2022-04-04
  • 兩個(gè)命令把 Vim 打造成 Python IDE的方法

    兩個(gè)命令把 Vim 打造成 Python IDE的方法

    這篇文章主要介紹了兩個(gè)命令把 Vim 打造成 Python IDE,需要的朋友可以參考下
    2016-03-03
  • 利用Python實(shí)現(xiàn)自制文件搜索小工具

    利用Python實(shí)現(xiàn)自制文件搜索小工具

    當(dāng)自己電腦文件很多還有點(diǎn)亂,不記得自己文件放哪里的時(shí)候,用電腦自帶的搜索文件,這個(gè)等待時(shí)間可慢了。所以我們不如自己用python做一個(gè)搜索工具!犄角旮旯的文件都能一秒鐘搜索出來(lái)的那種
    2022-09-09

最新評(píng)論

富阳市| 天水市| 锡林浩特市| 双桥区| 东方市| 聊城市| 舟曲县| 南澳县| 庆元县| 台江县| 闻喜县| 石台县| 仙桃市| 高要市| 民乐县| 翁源县| 邹城市| 金阳县| 华坪县| 娱乐| 馆陶县| 齐齐哈尔市| 马龙县| 米林县| 寻甸| 武胜县| 大城县| 广汉市| 丰城市| 福清市| 佛教| 北流市| 平山县| 宣武区| 兴山县| 平潭县| 韶关市| 黑水县| 博野县| 邳州市| 沾化县|