Skip to content

Instantly share code, notes, and snippets.

@lxl66566
Created May 11, 2024 04:42
Show Gist options
  • Save lxl66566/cf29d98741a78a164d5ad2cfb4aa92a7 to your computer and use it in GitHub Desktop.
Save lxl66566/cf29d98741a78a164d5ad2cfb4aa92a7 to your computer and use it in GitHub Desktop.
Typst table with merged cells from excel (forwarded)
import win32com.client
excel = win32com.client.GetActiveObject("Excel.Application")
wb = excel.ActiveWorkbook
sheet = wb.ActiveSheet
selected = excel.Selection
columns = selected.Columns.Count
out = open("output.typ", "w", encoding="utf-8")
out.write(f"#table(columns: {columns}, ")
for i in range(selected.Rows.Count):
for j in range(selected.Columns.Count):
cell = selected.Cells(i + 1, j + 1)
if cell.MergeCells:
is_top_left = cell.Address == cell.MergeArea.Cells(1, 1).Address
if is_top_left:
rows = cell.MergeArea.Rows.Count
cols = cell.MergeArea.Columns.Count
value = cell.Text
out.write("table.cell(")
if rows > 1:
out.write(f"rowspan: {rows}, ")
if cols > 1:
out.write(f"colspan: {cols}, ")
out.seek(out.tell() - 2)
out.write(f")[{value}], ")
else:
pass
else:
value = cell.Text
out.write(f"[{value}], ")
out.seek(out.tell() - 2)
out.write(")\n")
out.close()
@lxl66566
Copy link
Author

usage:

  1. install python and pywin32 module
  2. open an excel, select table region you want to introduce
  3. run this script, and view output.typ

@lxl66566
Copy link
Author

Forwarded from Tencent QQ user: obj.fake_cirno (ID: flarib1149). Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment