Skip to content

Instantly share code, notes, and snippets.

@MShrimp4
Last active December 26, 2023 04:00
Show Gist options
  • Save MShrimp4/6464248af09ac2c8baffdf128d52f210 to your computer and use it in GitHub Desktop.
Save MShrimp4/6464248af09ac2c8baffdf128d52f210 to your computer and use it in GitHub Desktop.
SGCC 사인머신 (대충짠코드)
PowerShell.exe -ExecutionPolicy Bypass -File .\everything-to-jpg.ps1
# https://imagemagick.org/script/download.php#windows
# If a motherhubbard uploaded pdf, also download https://ghostscript.com/releases/gsdnld.html (64bit)
mkdir jpg
Get-ChildItem -File | Foreach {magick convert $_.name ".\\jpg\\$((Get-Item $_.fullname).Basename).jpg"}
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options
import urllib, re, time, os, shutil
signimgpath = os.path.join(
os.path.dirname(__file__),
"signs")
fallbackdownloadpath = os.path.join(
os.path.dirname(__file__),
"fdownload")
if not os.path.exists(signimgpath):
os.mkdir(signimgpath)
if os.path.exists(fallbackdownloadpath):
shutil.rmtree(fallbackdownloadpath, )
os.mkdir(fallbackdownloadpath)
options = Options()
options.set_preference("browser.download.folderList", 2)
options.set_preference("browser.download.manager.showWhenStarting", False)
options.set_preference("browser.download.dir", fallbackdownloadpath)
options.set_preference("browser.download.useDownloadDir", False)
options.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/x-gzip")
driver = webdriver.Firefox(options=options)
"""
name-link.csv example
<저장할 때 "쉼표로 분리된 파일(.csv) UTF-8 포맷"으로>
갯가재,https://drive.google.com/open?id=somethingsomething
"""
def gen_path(name, ext = None):
if ext == None:
ext = re.search(r"-\s+.+(\.\w+)\s+- Google Drive", driver.title).group(1)
savename = name.strip() + ext
print(savename)
savepath = os.path.join(signimgpath, savename)
return savepath
retry = 10
def download(name):
element = False
for i in range(retry):
try:
element = driver.find_element(By.XPATH, '//img[starts-with(@src,"https://lh3.googleusercontent.com/drive-viewer/")]')
img_link = element.get_property("src")
savepath = gen_path(name)
urllib.request.urlretrieve(img_link, savepath)
break
except:
pass
time.sleep(1)
else: ## Fallback
savepath = gen_path(name)
driver.find_element(By.XPATH, "//div[@aria-label='다운로드']").click()
time.sleep(10)
print(f"I will kill {name}")
fname = os.path.join(fallbackdownloadpath, os.listdir(fallbackdownloadpath)[0])
shutil.move(fname, savepath)
f = open("name-link.csv", encoding="utf-8-sig")
for (name, l) in [tuple(line.split(',')) for line in f.readlines()]:
print(f"name: {name}, l:{l}")
driver.get(l)
##wait
time.sleep(5)
download(name.strip())
driver.close()
import docx
from docx.shared import Cm
import os
signimgpath = os.path.join(
os.path.dirname(__file__),
"signs")
path = os.path.join(signimgpath, "jpg")
doc = os.path.join(signimgpath, "signs.docx")
namef = open("name-link.csv", encoding="utf-8-sig")
d = docx.Document()
names = [i.split(',')[0] for i in namef.readlines()]
l = len(names)
print(f"l = {l}")
t = d.add_table(l, 2)
namef.close()
for (i,name) in enumerate(names):
"""
CELL 0 : Name (font:Gulim)
"""
namep = t.cell(i,0).paragraphs[0]
namer = namep.add_run(name.strip())
namef = namer.font
namef.name = "Gulim"
"""
CELL 1 : Image (height: 1.6cm)
"""
c = t.cell(i,1)
p = c.paragraphs[0]
r = p.add_run()
iname = os.path.join(path , name.strip() + ".jpg")
print(f"name = {iname}")
r.add_picture(iname,height=Cm(1.6))
d.save(doc)
@MShrimp4
Copy link
Author

pip install selenium
pip install python-docx

.ps1 스크립트는 imagemagick 필요

@MShrimp4
Copy link
Author

get-signs.py -> everything-to-jpg.bat -> signs-to-table.py

순서대로 하면 되고 결과물은 ./signs/signs.docx 로 나온다

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