我有使用Python DOCX圖書館麻煩,我已經刮從網站圖片,我想將它們添加到DOCX,但我不能添加的圖片就能夠直接DOCX,我不斷收到一個錯誤:如何從URL添加圖片到docx python?
File "C:\Python27\lib\site-packages\docx\image\image.py", line 46, in from_file with open(path, 'rb') as f: IOError: [Errno 22] invalid mode ('rb') or filename: ' http://upsats.com/Content/Product/img/Product/Thumb/PCB2x8-.jpg '
這是我的代碼:
import urllib
import requests
from bs4 import BeautifulSoup
from docx import Document
from docx.shared import Inches
import os
document = Document()
document.add_heading("Megatronics Items Full Search", 0)
FullPage = ['New-Arrivals-2017-6', 'Big-Sales-click-here', 'Arduino-Development-boards',
'Robotics-and-Copters', 'Breakout-Boards', 'RC-Wireless-communication', 'GSM,-GPS,-RFID,-Wifi',
'Advance-Development-boards-and-starter-Kits', 'Sensors-and-IMU', 'Solenoid-valves,-Relays,--Switches',
'Motors,-drivers,-wheels', 'Microcontrollers-and-Educational-items', 'Arduino-Shields',
'Connectivity-Interfaces', 'Power-supplies,-Batteries-and-Chargers', 'Programmers-and-debuggers',
'LCD,-LED,-Cameras', 'Discrete-components-IC', 'Science-Education-and-DIY', 'Consumer-Electronics-and-tools',
'Mechanical-parts', '3D-Printing-and-CNC-machines', 'ATS', 'UPS', 'Internal-Battries-UPS',
'External-Battries-UPS']
urlp1 = "http://www.arduinopak.com/Prd.aspx?Cat_Name="
URL = urlp1 + FullPage[0]
for n in FullPage:
URL = urlp1 + n
page = urllib.urlopen(URL)
bsObj = BeautifulSoup(page, "lxml")
panel = bsObj.findAll("div", {"class": "panel"})
for div in panel:
titleList = div.find('div', attrs={'class': 'panel-heading'})
imageList = div.find('div', attrs={'class': 'pro-image'})
descList = div.find('div', attrs={'class': 'pro-desc'})
r = requests.get("http://upsats.com/", stream=True)
data = r.text
for link in imageList.find_all('img'):
image = link.get("src")
image_name = os.path.split(image)[1]
r2 = requests.get(image)
with open(image_name, "wb") as f:
f.write(r2.content)
print(titleList.get_text(separator=u' '))
print(imageList.get_text(separator=u''))
print(descList.get_text(separator=u' '))
document.add_heading("%s \n" % titleList.get_text(separator=u' '))
document.add_picture(image, width=Inches(1.5))
document.add_paragraph("%s \n" % descList.get_text(separator=u' '))
document.save('megapy.docx')
不是全部,而只是主要部分。現在,我在複製下載的圖片時遇到問題,我想將其複製到docx。我不知道如何添加圖片。我如何轉換它?我想我必須格式化,但我該怎麼做?
我所知道的就是問題之所在這段代碼中:
document.add_picture(image, width=Inches(1.0))
如何使這一形象從URL中的docx顯示?我錯過了什麼?
的可能的複製[在一個特定位置上的文檔(.docx)與Python在添加圖像?](https://開頭stackoverflow.com/questions/32932230/add-an-image-in-a-specific-position-in-the-document-docx-with-python) – Veltro
對不起,但這是定位。我想在docx文件中顯示圖像,我已經從這個網址下載了圖像:www.arduinopak.com/,但是我無法將圖像存入docx文件。 –