首页 > 解决方案 > 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!

标签: pythoncmd

解决方案


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


推荐阅读