我无法解决这个问题。它抱怨wb.save()行。我不知道在敲了敲头之后是什么导致了它。我怀疑它与尝试打开空白页并在进行格式化操作后保存它有关,但我无法想象我在那里做了什么导致了这个问题。当我打开现有的电子表格并进行操作时,它工作正常,但这要求我首先拥有一个现有的电子表格。在这里,我试图从头开始一个新的电子表格。
from bs4 import BeautifulSoup
from lxml import etree
import os, codecs
import imageFilesSub
import re
import openpyxl, lxml
from openpyxl.utils import get_column_letter, column_index_from_string
homeEnv = 0 # 1 - home, 0 - work
if homeEnv:
filesDir = r'K:\Users\Johnny\My Documents\_World_of_Waterfalls\Website\tier 2 pages\tier 3 pages\tier 4 pages'
filesOutDir = r'K:\Users\Johnny\My Documents\_World_of_Waterfalls\WordPressSite'
else:
filesDir = r'..\old_travelblog_writeups'
filesOutDir = r'./'
# First get the list of files to parse
filesInDir = os.listdir(filesDir)
filesToParse = []
for file in filesInDir:
if ('travel-blog' in file) and (file.endswith('-template.html')):
filesToParse.append(file)
# Open up the travelBlog spreadsheet and set it up
wb = openpyxl.Workbook()
sheet = wb.active
sheet.name = "travelBlog List"
sheet['A1'].value = 'Blog No.'
sheet['B1'].value = 'Title'
sheet['C1'].value = 'Category'
sheet['D1'].value = 'Keyword Tags'
sheet['E1'].value = 'Excerpt'
sheet['F1'].value = 'Featured Image Filename'
sheet['G1'].value = 'Featured Image Alt Text'
sheet['H1'].value = 'Start Date'
sheet['I1'].value = 'End Date'
sheet['J1'].value = 'Old Web Address'
sheet['K1'].value = 'New Travel Blog Body Filename'
sheet['L1'].value = 'Old Travel Blog Template to parse'
sheet.freeze_panes = 'C2'
sheet.column_dimensions['A'].width = 10
sheet.column_dimensions['H','I'] = 20
sheet.column_dimensions['B','F','J','K','L'] = 40
sheet.column_dimensions['D','E'] = 50
from openpyxl.styles import Font
headerFontObj = Font(name='Arial', bold=True)
for col in range(1,sheet.max_column):
sheet.cell(row=1, column=col).font = headerFontObj
wb.save('travelBlogParsed.xlsx')
提前致谢, 约翰尼
答案 0 :(得分:5)
我明白了。问题如下:
sheet.column_dimensions['H','I'] = 20
sheet.column_dimensions['B','F','J','K','L'] = 40
sheet.column_dimensions['D','E'] = 50
首先,显然,您无法在向量上使用column_dimensions方法。参数必须是一个字符串。其次,这些行缺少.width属性。那些线应该是:
sheet.column_dimensions['H'].width = 20
sheet.column_dimensions['I'].width = 20
sheet.column_dimensions['B'].width = 40
...
sheet.column_dimensions['E'].width = 50