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

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

Use xlsxwriter write_url with excel file opened through openpyxl

问题

  1. def linkCells():
  2. wb = load_workbook(filename='output1.xlsx')
  3. ws = wb['Summary']
  4. cells = []
  5. for row in ws.iter_rows():
  6. for cell in row:
  7. temp = re.compile("([a-zA-Z]+)([0-9]+)")
  8. res = temp.match(cell.coordinate).groups()
  9. val1 = res[0]
  10. val2 = int(res[1])
  11. if val1 == "B" and val2 >= 25:
  12. cells.append(cell)
  13. for val in cells:
  14. cellCordinate = val.coordinate
  15. temp = re.compile("([a-zA-Z]+)([0-9]+)")
  16. res = temp.match(cellCordinate).groups()
  17. val2 = int(res[1])
  18. cellValue = val.value[:30]
  19. link = "output1.xlsx#" + cellValue + "!A1"
  20. ws.cell(2, val2).hyperlink = link
  21. ws.cell(2, val2).style = "Hyperlink"
  22. 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:

  1. def linkCells():
  2. wb = load_workbook(filename = 'output1.xlsx')
  3. ws = wb['Summary']
  4. cells = []
  5. for row in ws.iter_rows():
  6. for cell in row:
  7. temp = re.compile("([a-zA-Z]+)([0-9]+)")
  8. res = temp.match(cell.coordinate).groups()
  9. val1 = res[0]
  10. val2 = int(res[1])
  11. if val1 == "B" and val2 >= 25:
  12. cells.append(cell)
  13. for val in cells:
  14. cellCordinate = val.coordinate
  15. temp = re.compile("([a-zA-Z]+)([0-9]+)")
  16. res = temp.match(cellCordinate).groups()
  17. val2 = int(res[1])
  18. cellValue = val.value[:30]
  19. link = "output1.xlsx#"+cellValue+"!A1"
  20. ws.cell(2,val2).hyperlink = link
  21. ws.cell(2,val2).style = "Hyperlink"
  22. 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

  1. 从我的角度看你只想要做这个
  2. ```python
  3. from openpyxl import load_workbook
  4. from openpyxl.worksheet.hyperlink import Hyperlink
  5. xlfile = "output1.xlsx"
  6. summary_sheet = 'Summary'
  7. wb = load_workbook(xlfile)
  8. ws = wb[summary_sheet]
  9. cell_coord = 'A1'
  10. for row, sh in enumerate(wb.worksheets):
  11. if sh.title == summary_sheet:
  12. continue
  13. hyperlink = Hyperlink(target=xlfile,
  14. ref=cell_coord,
  15. location=f'{sh.title}!{cell_coord}'
  16. )
  17. ws.cell(row, 1).value = f"{sh.title}"
  18. ws.cell(row, 1).hyperlink = hyperlink
  19. wb.save(xlfile)

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

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

  1. <details>
  2. <summary>英文:</summary>
  3. 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)

  1. 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;
  2. [![Example output Excel file][1]][1]
  3. [1]: https://i.stack.imgur.com/L2N1B.png
  4. </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:

确定