使用 openpyxl 打开的 Excel 文件,使用 xlsxwriter 的 write_url 写入链接。

huangapple go评论74阅读模式
英文:

Use xlsxwriter write_url with excel file opened through openpyxl

问题

def linkCells():
    wb = load_workbook(filename='output1.xlsx')
    ws = wb['Summary']
    cells = []
    for row in ws.iter_rows():
        for cell in row:
            temp = re.compile("([a-zA-Z]+)([0-9]+)")
            res = temp.match(cell.coordinate).groups()
            val1 = res[0]
            val2 = int(res[1])
            if val1 == "B" and val2 >= 25:
                cells.append(cell)
    for val in cells:
        cellCordinate = val.coordinate
        temp = re.compile("([a-zA-Z]+)([0-9]+)")
        res = temp.match(cellCordinate).groups()
        val2 = int(res[1])
        cellValue = val.value[:30]
        link = "output1.xlsx#" + cellValue + "!A1"
        ws.cell(2, val2).hyperlink = link
        ws.cell(2, val2).style = "Hyperlink"
    wb.save(filename='output1.xlsx')
英文:

I want to add links to a sheet in the same workbook. The sheet with the link is "Summary" and would contain multiple links pointing to different sheets in the same workbook.

Here is the complete code:

def linkCells():
    wb = load_workbook(filename = 'output1.xlsx')
    ws = wb['Summary']
    cells = []
    for row in ws.iter_rows():
        for cell in row:
            temp = re.compile("([a-zA-Z]+)([0-9]+)")
            res = temp.match(cell.coordinate).groups()
            val1 = res[0]
            val2 = int(res[1])
            if val1 == "B" and val2 >= 25:
                cells.append(cell)
    for val in cells:
        cellCordinate = val.coordinate
        temp = re.compile("([a-zA-Z]+)([0-9]+)")
        res = temp.match(cellCordinate).groups()
        val2 = int(res[1])
        cellValue = val.value[:30]
        link = "output1.xlsx#"+cellValue+"!A1"
        ws.cell(2,val2).hyperlink = link
        ws.cell(2,val2).style = "Hyperlink"
    wb.save(filename = 'output1.xlsx')

But based on the following link:

https://stackoverflow.com/questions/30110511/create-a-hyperlink-to-a-different-excel-sheet-in-the-same-workbook

Support for hyperlinks in openpyxl is currently extremely rudimentary and largely limited to reading the links in existing files.

I also know that xlsxwriter has write_url function which could help to create links, but from my understanding, there is no way to open an existing file in xlsxwriter.

Is there any way my given code could work in openpyxl or how can I open the file using openpyxl and then create links using xlsxwriter(I don't think the second option is feasible as far as I know)?

答案1

得分: 1

从我的角度看你只想要做这个

```python
from openpyxl import load_workbook
from openpyxl.worksheet.hyperlink import Hyperlink

xlfile = "output1.xlsx"
summary_sheet = 'Summary'

wb = load_workbook(xlfile)
ws = wb[summary_sheet]

cell_coord = 'A1'

for row, sh in enumerate(wb.worksheets):
    if sh.title == summary_sheet:
        continue
    hyperlink = Hyperlink(target=xlfile,
                          ref=cell_coord,
                          location=f'{sh.title}!{cell_coord}'
                         )
    ws.cell(row, 1).value = f"{sh.title}"
    ws.cell(row, 1).hyperlink = hyperlink

wb.save(xlfile)

输出文件在每个工作表1-4的A列中都有4个链接。如果单击链接,它会将焦点移动到工作簿中所选工作表的单元格'A1'。

使用 openpyxl 打开的 Excel 文件,使用 xlsxwriter 的 write_url 写入链接。


<details>
<summary>英文:</summary>

Seems to me all you want to do is this 


from openpyxl import load_workbook
from openpyxl.worksheet.hyperlink import Hyperlink

xlfile = "output1.xlsx"
summary_sheet = 'Summary'

wb = load_workbook(xlfile)
ws = wb[summary_sheet]

cell_coord = 'A1'

for row, sh in enumerate(wb.worksheets):
if sh.title == summary_sheet:
continue
hyperlink = Hyperlink(target=xlfile,
ref=cell_coord,
location=f'{sh.title}!{cell_coord}'
)
ws.cell(row, 1).value = f"{sh.title}"
ws.cell(row, 1).hyperlink = hyperlink

wb.save(xlfile)


The output file has 4 links in column A for each of the Sheets 1-4. If you click a link it will move the focus to cell &#39;A1&#39; of the selected sheet in the workbook.&lt;br&gt;
 
[![Example output Excel file][1]][1]


  [1]: https://i.stack.imgur.com/L2N1B.png

</details>



huangapple
  • 本文由 发表于 2023年7月6日 22:12:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76629743.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定