Created
May 11, 2024 04:42
-
-
Save lxl66566/cf29d98741a78a164d5ad2cfb4aa92a7 to your computer and use it in GitHub Desktop.
Typst table with merged cells from excel (forwarded)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
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
usage:
output.typ