找回密码
 快速注册
搜索
查看: 54|回复: 7

[Windows shell] Inkscape convert .wmf to .svg 批量

[复制链接]

3149

主题

8386

回帖

6万

积分

$\style{scale:11;fill:#eff}꩜$

积分
65391
QQ

显示全部楼层

hbghlyj 发表于 2023-3-6 00:12 |阅读模式
使用Johannski写的batch 脚本即可
What file type do you want touse as a source?
输入wmf
What file type do you want to convert to?
输入svg
1-min.png
等待转换完毕
1-min.png

3149

主题

8386

回帖

6万

积分

$\style{scale:11;fill:#eff}꩜$

积分
65391
QQ

显示全部楼层

 楼主| hbghlyj 发表于 2023-3-6 00:18
本帖最后由 hbghlyj 于 2023-3-10 22:37 编辑 Batch script 使用SVGO批量对当前目录下svg进行优化: 直接在命令行输入
  1. for %i in (*.svg) do svgo "%i"
复制代码

使用 下面的 python 将svg转换为tikz并去除多余换行
  1. from svg2tikz import convert_svg
  2. from re import sub
  3. import glob,os
  4. for file in glob.glob("*.svg"):
  5.     code = convert_svg(file,codeoutput="figonly",texmode="math")
  6.     code = sub(r'\n+',r'\n',code)
  7.     code = code.replace('\n\\definecolor{c010101}{RGB}{1,1,1}','').replace(r'yscale=\globalscale, xscale=\globalscale',r'scale=\globalscale')
  8.     code = sub(r"\\path\[draw=c010101,dash[^]]*\]",r'\\draw[dashed]',code)
  9.     code = sub(r"\\path\[draw=c010101[^]]*\]",r'\\draw',code)
  10.     with open(os.path.splitext(file)[0]+'.txt', "w") as file:
  11.         file.write(code.strip())
复制代码

效果:
1-min.png

3149

主题

8386

回帖

6万

积分

$\style{scale:11;fill:#eff}꩜$

积分
65391
QQ

显示全部楼层

 楼主| hbghlyj 发表于 2023-3-6 01:52

[Python]将pandoc转换出来的巨大html 在每个只含<li>的行分开

  1. # Open the input file and read its contents
  2. with open('large_file.html', 'r') as input_file:
  3.     contents = input_file.readlines()
  4. # Create a list to store the lines for each output file
  5. output_lists = [[]]
  6. # Loop through the lines of the input file
  7. for line in contents:
  8.     # If the line starts with "<li>", create a new output file
  9.     if line.startswith('<li>'):
  10.         output_lists.append([])
  11.     # Add the line to the current output list
  12.     output_lists[-1].append(line)
  13. # Loop through the output lists and write them to separate files
  14. for i, output_list in enumerate(output_lists):
  15.     with open(f'{i+1}.html', 'w') as output_file:
  16.         # Write all the lines in the output list except the "<li>" lines
  17.         output_file.writelines(line for line in output_list if not line.startswith('<li>'))
复制代码

输出:
1-min.png

3149

主题

8386

回帖

6万

积分

$\style{scale:11;fill:#eff}꩜$

积分
65391
QQ

显示全部楼层

 楼主| hbghlyj 发表于 2023-3-6 02:43

[Python] replace string from all .txt files in PWD

remove all occurrences of the string y=1cm, x=1cm, from all .txt files in the current working directory:
  1. import os
  2. import re
  3. # Set the search string
  4. search_str = r'y=1cm, x=1cm, '
  5. # Loop through all files in the current directory
  6. for filename in os.listdir(os.getcwd()):
  7.     # Check if the file is a txt file
  8.     if filename.endswith('.txt'):
  9.         # Open the file and read its contents
  10.         with open(filename, 'r') as f:
  11.             contents = f.read()
  12.         
  13.         # Replace all occurrences of the search string with an empty string
  14.         new_contents = re.sub(search_str, '', contents)
  15.         
  16.         # Write the new contents back to the file
  17.         with open(filename, 'w') as f:
  18.             f.write(new_contents)
复制代码

3149

主题

8386

回帖

6万

积分

$\style{scale:11;fill:#eff}꩜$

积分
65391
QQ

显示全部楼层

 楼主| hbghlyj 发表于 2023-3-6 04:02
Installing for use with Inkscape
Inkscape extension: Export to Tikz path
Quicker_20230305_204014.png
In Known Problems of tikz2svg:
Grouped elements will not work. So ungroup everything

Inkscape ungroup everything from windows command line
  1. @echo off
  2. for %%i in ("%~dp0*.svg") do (
  3.     "D:\Inkscape\bin\inkscape.exe" -f "%%i" --verb EditSelectAll --verb SelectionUnGroup --verb FileSave --verb FileQuit
  4. )
复制代码

3149

主题

8386

回帖

6万

积分

$\style{scale:11;fill:#eff}꩜$

积分
65391
QQ

显示全部楼层

 楼主| hbghlyj 发表于 2023-3-6 06:42

convert TikZ shift option of each \path to \\begin{scope}...\\end{scope}



  1. import os
  2. import re
  3. def process_tex_file(file_path):
  4. # Open the file and read its contents
  5. with open(file_path, 'r') as f:
  6. contents = f.readlines()
  7. # Loop through the lines in the file
  8. i = 0
  9. while i < len(contents):
  10. line = contents[i]
  11. # Search for the pattern
  12. match = re.search(r'\[(shift=\{.+?\}),fill=black\]', line)
  13. if match:
  14. # Insert the scope before the matched line
  15. scope = match.group(1)
  16. contents.insert(i, f'\\begin{{scope}}[{scope}]\n')
  17. # Delete the pattern from the line
  18. contents[i+1] = re.sub(r'\[(shift=\{.+?\}),fill=black\]', '', contents[i+1])
  19. i += 1 # Advance to the next line
  20. # Keep replacing the pattern till the end of the file
  21. while i < len(contents):
  22. line = contents[i]
  23. contents[i] = re.sub(r'\[(shift=\{.+?\}),fill=black\]', '', line)
  24. i += 1
  25. break # Stop searching after the first match
  26. i += 1 # Advance to the next line
  27. # Insert the \end{scope} command before the last line
  28. contents.insert(-1, r'\end{scope}'+'\n')
  29. # Write the modified contents back to the file
  30. with open('converted/'+file_path, 'w') as f:
  31. f.writelines(contents)
  32. # Loop through all .txt files in the current directory
  33. for file_name in os.listdir('.'):
  34. if file_name.endswith('.txt'):
  35. file_path = os.path.join('.', file_name)
  36. process_tex_file(file_path)
复制代码
This code processes all .txt files in the current directory by searching for the first occurrence of the pattern [shift={something},fill=black] in each file. If a match is found, it inserts a \begin{scope} command before the matched line and deletes the pattern from that line. It then continues to replace the pattern with an empty string until the end of the file. After this, it inserts an \end{scope} command before the last line in the file. The modified contents are then written a file in converted/ folder of the same name. This process is repeated for each .txt file in the directory.

Example

The original fileThe converted file
\begin{tikzpicture}[scale=.2, inner sep=0pt, outer sep=0pt]
\draw (1.1000,2.8000) -- (17.3000,2.8000)(17.3000,2.8000) -- (9.0000,24.9000)(9.0000,24.9000) -- (1.1000,2.8000);
\draw (9.1500,8.4500) circle (0.1623cm);
\draw (9.0000,24.9000) -- (9.0000,2.8000)(3.8000,10.5000) -- (14.5000,10.5000)(1.1000,2.8000) -- (15.4000,8.0000)(17.3000,2.8000) -- (2.9000,8.0000)(2.9000,8.0000) -- (15.4000,8.0000)(9.0000,2.8000) -- (4.7000,13.0000)(9.0000,2.8000) -- (3.8000,10.5000)(9.0000,2.8000) -- (2.9000,8.0000)(4.7000,12.9000) -- (13.5000,12.9000)(10.4000,12.9000) -- (16.3000,12.9000)(5.4000,11.3000) -- (3.2000,16.7000);
\path[shift={(2.2,8.100000000000001)},fill=black] (0.0000,26.3980) node[above right] () {3};
\path[shift={(2.2,8.100000000000001)},fill=black] (0.8000,31.9416) node[above right] () {K};
\path[shift={(2.2,8.100000000000001)},fill=black] (8.3000,20.2416) node[above right] () {K};
\path[shift={(2.2,8.100000000000001)},fill=black] (6.5000,44.6416) node[above right] () {A};
\path[shift={(2.2,8.100000000000001)},fill=black] (9.1000,20.0980) node[above right] () {1};
\path[shift={(2.2,8.100000000000001)},fill=black] (6.7000,20.0980) node[above right] () {1};
\path[shift={(2.2,8.100000000000001)},fill=black] (5.3000,20.0980) node[above right] () {1};
\path[shift={(2.2,8.100000000000001)},fill=black] (15.0000,20.5416) node[above right] () {C};
\path[shift={(2.2,8.100000000000001)},fill=black] (12.6000,29.2416) node[above right] () {T};
\path[shift={(2.2,8.100000000000001)},fill=black] (0.0000,29.6416) node[above right] () {T};
\path[shift={(2.2,8.100000000000001)},fill=black] (6.2000,20.3416) node[above right] () {T};
\path[shift={(2.2,8.100000000000001)},fill=black] (4.4000,20.3416) node[above right] () {H};
\path[shift={(2.2,8.100000000000001)},fill=black] (7.3000,25.6416) node[above right] () {I};
\path[shift={(2.2,8.100000000000001)},fill=black] (-1.7000,20.5416) node[above right] () {B};
\path[shift={(2.2,8.100000000000001)},fill=black] (8.0000,20.4416) node[above right] () {(    )};
\path[shift={(2.2,8.100000000000001)},fill=black] (5.8000,20.4416) node[above right] () {(    )};
\path[shift={(2.2,8.100000000000001)},fill=black] (13.5000,26.5416) node[above right] () {H};
\path[shift={(2.2,8.100000000000001)},fill=black] (-0.8000,26.6416) node[above right] () {H};
\path[shift={(2.2,8.100000000000001)},fill=black] (1.9000,34.9980) node[above right] () {2};
\path[shift={(2.2,8.100000000000001)},fill=black] (13.3000,28.8980) node[above right] () {2};
\path[shift={(2.2,8.100000000000001)},fill=black] (14.4000,26.1980) node[above right] () {2};
\path[shift={(2.2,8.100000000000001)},fill=black] (14.4000,32.1980) node[above right] () {1};
\path[shift={(2.2,8.100000000000001)},fill=black] (1.8000,31.8980) node[above right] () {3};
\path[shift={(2.2,8.100000000000001)},fill=black] (0.8000,29.3980) node[above right] () {3};
\path[shift={(2.2,8.100000000000001)},fill=black] (13.9000,32.4416) node[above right] () {l};
\path[shift={(2.2,8.100000000000001)},fill=black] (1.5000,35.2416) node[above right] () {l};
\end{tikzpicture}
\begin{tikzpicture}[scale=.2, inner sep=0pt, outer sep=0pt]
\draw (1.1000,2.8000) -- (17.3000,2.8000)(17.3000,2.8000) -- (9.0000,24.9000)(9.0000,24.9000) -- (1.1000,2.8000);
\draw (9.1500,8.4500) circle (0.1623cm);
\draw (9.0000,24.9000) -- (9.0000,2.8000)(3.8000,10.5000) -- (14.5000,10.5000)(1.1000,2.8000) -- (15.4000,8.0000)(17.3000,2.8000) -- (2.9000,8.0000)(2.9000,8.0000) -- (15.4000,8.0000)(9.0000,2.8000) -- (4.7000,13.0000)(9.0000,2.8000) -- (3.8000,10.5000)(9.0000,2.8000) -- (2.9000,8.0000)(4.7000,12.9000) -- (13.5000,12.9000)(10.4000,12.9000) -- (16.3000,12.9000)(5.4000,11.3000) -- (3.2000,16.7000);
\begin{scope}[shift={(2.2,8.100000000000001)}]
\path (0.0000,26.3980) node[above right] () {3};
\path (0.8000,31.9416) node[above right] () {K};
\path (8.3000,20.2416) node[above right] () {K};
\path (6.5000,44.6416) node[above right] () {A};
\path (9.1000,20.0980) node[above right] () {1};
\path (6.7000,20.0980) node[above right] () {1};
\path (5.3000,20.0980) node[above right] () {1};
\path (15.0000,20.5416) node[above right] () {C};
\path (12.6000,29.2416) node[above right] () {T};
\path (0.0000,29.6416) node[above right] () {T};
\path (6.2000,20.3416) node[above right] () {T};
\path (4.4000,20.3416) node[above right] () {H};
\path (7.3000,25.6416) node[above right] () {I};
\path (-1.7000,20.5416) node[above right] () {B};
\path (8.0000,20.4416) node[above right] () {(    )};
\path (5.8000,20.4416) node[above right] () {(    )};
\path (13.5000,26.5416) node[above right] () {H};
\path (-0.8000,26.6416) node[above right] () {H};
\path (1.9000,34.9980) node[above right] () {2};
\path (13.3000,28.8980) node[above right] () {2};
\path (14.4000,26.1980) node[above right] () {2};
\path (14.4000,32.1980) node[above right] () {1};
\path (1.8000,31.8980) node[above right] () {3};
\path (0.8000,29.3980) node[above right] () {3};
\path (13.9000,32.4416) node[above right] () {l};
\path (1.5000,35.2416) node[above right] () {l};
\end{scope}
\end{tikzpicture}

3149

主题

8386

回帖

6万

积分

$\style{scale:11;fill:#eff}꩜$

积分
65391
QQ

显示全部楼层

 楼主| hbghlyj 发表于 2023-3-6 07:04

将\path... 替换为\node at... 使TikzEdt可以识别

本帖最后由 hbghlyj 于 2023-3-10 22:18 编辑
  1. import os
  2. import re
  3. def process_tex_file(file_path):
  4.     # Open the file and read its contents
  5.     with open(file_path, 'r') as f:
  6.         contents = f.readlines()
  7.     # Loop through the lines in the file
  8.     i = 0
  9.     while i < len(contents):
  10.         line = contents[i]
  11.         # Search for the pattern
  12.         match = re.search(r'^\\path', line)
  13.         if match:
  14.             # Insert the scope before the matched line
  15.             contents[i]=re.sub('node','',contents[i])
  16.             contents[i]=re.sub(r'path','node at',contents[i])
  17.             contents[i]=re.sub(r' \(\) ','',contents[i])
  18.         i += 1  # Advance to the next line
  19.     # Write the modified contents back to the file
  20.     with open('converted/'+file_path, 'w') as f:
  21.         f.writelines(contents)
  22. # Loop through all .txt files in the current directory
  23. for file_name in os.listdir('.'):
  24.     if file_name.endswith('.txt'):
  25.         file_path = os.path.join('.', file_name)
  26.         process_tex_file(file_path)
复制代码

3149

主题

8386

回帖

6万

积分

$\style{scale:11;fill:#eff}꩜$

积分
65391
QQ

显示全部楼层

 楼主| hbghlyj 发表于 2023-3-6 08:03

含有\bullet的行, 去除[above right]

  1. import os
  2. # loop through all .txt files in the current directory
  3. for filename in os.listdir():
  4.     if filename.endswith('.txt'):
  5.         # open the original file for reading
  6.         with open(filename, 'r') as original_file:
  7.             # create a new file in the 'converted' directory for writing
  8.             with open(os.path.join('converted', filename), 'w') as converted_file:
  9.                 # loop through each line in the original file
  10.                 for line in original_file:
  11.                     # if the line contains '$\bullet$' and '[above right]', replace '[above right]' with an empty string
  12.                     if r'$\bullet$' in line and '[above right]' in line:
  13.                         line = line.replace('[above right]', '')
  14.                     # write the modified line to the new file
  15.                     converted_file.write(line)
复制代码

手机版|悠闲数学娱乐论坛(第3版)

GMT+8, 2025-3-4 15:24

Powered by Discuz!

× 快速回复 返回顶部 返回列表