python - Execute lines instead of print (python & cmd)
问题描述
Was browsing the internet for the past two hours and didn't find the answer. Ridiculus, I know ... Will write a bit more about it, maybe someone else finds this helpful as well.
I want to save every single mailchimp campaign as an unpaginated (continuous) PDF. Found this great program that does the job: wkhtmltopdf. All I have to do is run a command in cmd that looks like this ...
"C:\Program Files\wkhtmltopdf\bin\"wkhtmltopdf -T 0 -B 0 --page-width 210mm --page-height 2970mm "www link" "C:\Temp\test.pdf"
This obviously makes very long continous PDF (10 pages) but I can batch process (crop, optimize, arhchive) them later with Acobe Acrobat Pro.
I made a csv file that holds a list of all the campaigns ever sent. For instance (the last three):
Datum;Naslov;Povezava
18.01.2019;KEPA VESELJA;https://mailchi.mp/61fadc2e06a7/nm9bhb5lc3-1575581
22.11.2018;V NOV KORAK;https://mailchi.mp/6fc2f0ef4346/nm9bhb5lc3-1557633
08.10.2018;VPIS 2019/20;https://mailchi.mp/9369428d0749/nm9bhb5lc3-1543093
I then wrote a short python code ...
import csv
code = """"C:\\Program Files\\wkhtmltopdf\\bin\\"wkhtmltopdf -T 0 -B 0 --page-width 210mm --page-height 2970mm {povezava} "C:\\Users\\gaspe\\Desktop\\Temp\\{datum} - {naslov}.pdf\""""
with open("C:\\Users\\gaspe\\Desktop\\mailchimp\\seznam.csv", "r") as csvfile:
reader = csv.reader(csvfile, delimiter=';')
next(csvfile) #skipping the first row
for row in reader:
link = row[2]
date = row[0]
new_date = str(date[6:10]+"-"+date[3:5]+"-"+date[0:2])
title = row[1].upper().replace('/','-').replace('!','').replace('?','')
print(code.format(povezava=link, datum=new_date, naslov=title))
... that reads every row in a csv file and prints something like this ...
"C:\Program Files\wkhtmltopdf\bin\"wkhtmltopdf -T 0 -B 0 --page-width 210mm --page-height 2970mm https://mailchi.mp/61fadc2e06a7/nm9bhb5lc3-1575581 "C:\Users\gaspe\Desktop\Temp\2019-01-18 - KEPA VESELJA.pdf"
"C:\Program Files\wkhtmltopdf\bin\"wkhtmltopdf -T 0 -B 0 --page-width 210mm --page-height 2970mm https://mailchi.mp/6fc2f0ef4346/nm9bhb5lc3-1557633 "C:\Users\gaspe\Desktop\Temp\2018-11-22 - V NOV KORAK.pdf"
"C:\Program Files\wkhtmltopdf\bin\"wkhtmltopdf -T 0 -B 0 --page-width 210mm --page-height 2970mm https://mailchi.mp/9369428d0749/nm9bhb5lc3-1543093 "C:\Users\gaspe\Desktop\Temp\2018-10-08 - VPIS 2019-20.pdf"
Now the grand question is: How do I make cmd run all the lines one after the other instead of just printing them out?
I know, I can just copy and paste the lines in cmd and it will execute them, but I would really appreciate learning how to do it the proper way :)
Thanks!
解决方案
You can use the subprocess module to run the commands from shell:
import csv
from subprocess import check_output
code = """"C:\\Program Files\\wkhtmltopdf\\bin\\"wkhtmltopdf -T 0 -B 0 --page-width 210mm --page-height 2970mm {povezava} "C:\\Users\\gaspe\\Desktop\\Temp\\{datum} - {naslov}.pdf\""""
with open("C:\\Users\\gaspe\\Desktop\\mailchimp\\seznam.csv", "r") as csvfile:
reader = csv.reader(csvfile, delimiter=';')
next(csvfile) #skipping the first row
for row in reader:
link = row[2]
date = row[0]
new_date = str(date[6:10]+"-"+date[3:5]+"-"+date[0:2])
title = row[1].upper().replace('/','-').replace('!','').replace('?','')
cmd = code.format(povezava=link, datum=new_date, naslov=title)
# print(cmd)
check_output(cmd, shell=True)
Reference: https://docs.python.org/3/library/subprocess.html
推荐阅读
- android - 如何修复tensorflow和android studio之间的图像预处理差异?
- javascript - 运行有关语法错误的nodejs代码时出错
- java - 泛型类型的 Guice 绑定不适用于“.class”
- android - 同步gradle的问题
- spring-boot - Spring Boot 中使用 url 编码字符的请求映射
- node.js - 验证节点的中间件
- javascript - 两个子组件之间的数据传输问题
- python - python中嵌套循环中的执行顺序
- javascript - 未应用样式变量 - 车把
- apache-beam - Dataflow 会在多长时间内记住 PubsubIO 中的属性 ID