wordpress批量发布文章(python-tk)
from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.compat import xmlrpc_client
from wordpress_xmlrpc.methods import media, posts
import importlib,sys
importlib.reload(sys)
from pydocx import PyDocX
from bs4 import BeautifulSoup
import base64
import os
import time
from tkinter import *
import tkinter as tk
from tkinter.messagebox import showinfo
import os
from win32com import client
def doc_to_docx(path):
if os.path.splitext(path)[1] == ".doc":
word = client.Dispatch('Word.Application')
doc = word.Documents.Open(path) # 目标路径下的文件
doc.SaveAs(os.path.splitext(path)[0] + ".docx", 12) # 转化后路径下的文件
doc.Close()
word.Quit()
def find_file(path, ext, file_list=[]):
dir = os.listdir(path)
for i in dir:
i = os.path.join(path, i)
if os.path.isdir(i):
find_file(i, ext, file_list)
else:
if ext == os.path.splitext(i)[1]:
file_list.append(i)
return file_list
def new_docx():
basedir =os.path.dirname(os.path.abspath(__file__))
dir_path = basedir+"\文章存放目录" # 批量转换文件夹
ext = ".doc"
file_list = find_file(dir_path, ext)
for file in file_list:
doc_to_docx(file)
t1.insert(END,'转换结束'+'\n')
def main():
#获取输入框中内容
var_url = e1.get()
user = e2.get()
psw = e3.get()
#分隔网址
data_url = var_url.split('+')
for url in data_url:
url = url + 'xmlrpc.php'
t1.insert(END, url+'------开始发布' + '\n')
t1.update()
push(url,user,psw)
t1.insert(END, url + '------发布结束' + '\n'+'----------------'+'\n')
def push(url,user,psw):
path = "./文章存放目录" # 文件夹目录
files = os.listdir(path) # 得到文件夹下的所有文件名称
for file in files:
try:
html = PyDocX.to_html(path + '/' + file)
html = html.replace('<style>.pydocx-caps {text-transform:uppercase}.pydocx-center {text-align:center}.pydocx-comment {color:blue}.pydocx-delete {color:red;text-decoration:line-through}.pydocx-hidden {visibility:hidden}.pydocx-insert {color:green}.pydocx-left {text-align:left}.pydocx-list-style-type-cardinalText {list-style-type:decimal}.pydocx-list-style-type-decimal {list-style-type:decimal}.pydocx-list-style-type-decimalEnclosedCircle {list-style-type:decimal}.pydocx-list-style-type-decimalEnclosedFullstop {list-style-type:decimal}.pydocx-list-style-type-decimalEnclosedParen {list-style-type:decimal}.pydocx-list-style-type-decimalZero {list-style-type:decimal-leading-zero}.pydocx-list-style-type-lowerLetter {list-style-type:lower-alpha}.pydocx-list-style-type-lowerRoman {list-style-type:lower-roman}.pydocx-list-style-type-none {list-style-type:none}.pydocx-list-style-type-ordinalText {list-style-type:decimal}.pydocx-list-style-type-upperLetter {list-style-type:upper-alpha}.pydocx-list-style-type-upperRoman {list-style-type:upper-roman}.pydocx-right {text-align:right}.pydocx-small-caps {font-variant:small-caps}.pydocx-strike {text-decoration:line-through}.pydocx-tab {display:inline-block;width:4em}.pydocx-underline {text-decoration:underline}body {margin:0px auto;width:49.61em}</style>','')
title = file[:-5]
post = WordPressPost()
post.title = title
post.content = html
post.post_status = 'publish'
wp = Client(url, user, psw)
#文章有图执行
try:
soup = BeautifulSoup(html, "lxml")
strs = soup.img['src']
base64_str = strs.split(',', 1)[1]
imgdata = base64.b64decode(base64_str)
with open( title+".jpg", mode='wb') as f:
f.write(imgdata)
# 上传的图片文件路径
filename = title+'.jpg'
data = {
'name': title+'.jpg',
'type': 'image/jpeg',
}
with open(filename, 'rb') as img:
data['bits'] = xmlrpc_client.Binary(img.read())
data['bits'] = imgdata
response = wp.call(media.UploadFile(data))
attachment_id = response['id']
# 缩略图的id
post.thumbnail = attachment_id
post.id = wp.call(posts.NewPost(post))
time.sleep(2)
t1.insert(END,title+'------成功发布'+'\n')
t1.update()
os.remove(title+'.jpg')
#文章无图执行
except:
post.id = wp.call(posts.NewPost(post))
t1.insert(END,title+'------成功发布'+'\n')
t1.update()
except:
pass
def reply1():
showinfo(title='版权信息', message='仅用做测试')
def reply2():
showinfo(title='作者联系方式', message='微信号:xxxx')
if __name__ == '__main__':
#创建窗口
window = tk.Tk()
window.title("WordPress发布助手 v1.0.1")
window.geometry('800x600')
menubar = Menu(window)
def callback():
pass
filemenu = Menu(menubar, tearoff=False)
filemenu.add_command(label="打开", command=callback)
filemenu.add_command(label="保存", command=callback)
filemenu.add_separator()
filemenu.add_command(label="退出", command=window.quit)
menubar.add_cascade(label="文件", menu=filemenu)
editmenu = Menu(menubar, tearoff=False)
editmenu.add_command(label="版权信息", command=reply1)
editmenu.add_command(label="联系作者", command=reply2)
menubar.add_cascade(label="关于", menu=editmenu)
window.config(menu=menubar)
#设置默认输入内容
admin = StringVar()
admin.set(r'admin')
password = StringVar()
password.set(r'xxxxx')
l1 = tk.Label(window,
text='批量输入网址 以+分隔:',
font=('Arial', 12),
width=20,
height=2)
l1.place(x=1, y=10)
# 创建输入框entry
e1 = tk.Entry(window, width=50,background='#d3fbfb')
e1.place(x=200, y=23)
#发布按钮
b1 = tk.Button(
window,
text='发布',
width=10,
command=main)
b1.place(x=600, y=21)
#转换按钮
b2 = tk.Button(
window,
text='一键doc转docx',
width=15,
command=new_docx)
b2.place(x=600, y=60)
# 用户名
l2 = tk.Label(window,
text='输入用户名:',
font=('Arial', 12),
width=20,
height=2)
l2.place(x=10, y=50)
# 创建输入框entry
e2 = tk.Entry(window, width=20, background='#d3fbfb',textvariable=admin)
e2.place(x=200, y=60)
# 密码
l3 = tk.Label(window,
text='输入密码:',
font=('Arial', 12),
width=20,
height=2)
l3.place(x=10, y=80)
# 创建输入框entry
e3 = tk.Entry(window, width=20, background='#d3fbfb',textvariable=password)
e3.place(x=200, y=90)
#展示框
t1 = tk.Text(window,font=('Times', 13),width=60,height=20,background='#d3fbfb')
t1.place(x=200, y=120)
window.mainloop()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
去GitHub编辑 (opens new window)
上次更新: 2022/10/21, 22:31:13