Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将大文本导出到excel(Office 2007)时,导出到excel单元格的字符不超过8203个_Python_Excel_Win32com - Fatal编程技术网

Python 将大文本导出到excel(Office 2007)时,导出到excel单元格的字符不超过8203个

Python 将大文本导出到excel(Office 2007)时,导出到excel单元格的字符不超过8203个,python,excel,win32com,Python,Excel,Win32com,将大文本导出到excel时,导出到excel单元格的字符不超过8203个。如果我使用openpyxl lib,我认为VBA或win32com.client有问题-导出正确 错误:(-2147352567,'CharStyleбба',(0,无,无,0,-2146827284),无) 我只有在Office2007中才有这个问题,所以只能使用Office2007 2016年office export运行良好 import win32com.client import os path_excell

将大文本导出到excel时,导出到excel单元格的字符不超过8203个。如果我使用
openpyxl lib
,我认为VBA或win32com.client有问题-导出正确

错误:(-2147352567,'CharStyleбба',(0,无,无,0,-2146827284),无)

我只有在Office2007中才有这个问题,所以只能使用Office2007

2016年office export运行良好

import win32com.client
import os


path_excell = 'c:\wa\Excel2.xlsx'
excel = win32com.client.Dispatch("Excel.Application")
if os.path.isfile(path_excell):
    wb = excel.Workbooks.Open(path_excell)
else:
    wb = excel.Workbooks.add
sheet = wb.ActiveSheet
new_str = '1|2|3|4|5|6|7|8|9'
new_str = new_str.split('|')
k = 8200
i = 1
try:
    while k<8400:
        s = 'v'*k
        new_str[7] = s
        sheet.Range(sheet.Cells(i, 1), sheet.Cells(i, 9)).Value = new_str
        k += 1
        i += 1
except Exception as err:
    print(f'Error: {str(err)}: max {k} char')
finally:
    if os.path.isfile(path_excell):
        wb.Save()
    else:
        wb.SaveAs(path_excell)
    wb.Close()
    excel.Quit()
导入win32com.client
导入操作系统
路径_excell='c:\wa\Excel2.xlsx'
excel=win32com.client.Dispatch(“excel.Application”)
如果os.path.isfile(path_excell):
wb=excel.Workbooks.Open(路径\u excel)
其他:
wb=excel.Workbooks.add
sheet=wb.ActiveSheet
新|街='1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9'
new_str=new_str.split(“|”)
k=8200
i=1
尝试:

而k对我来说很好

import win32com.client
import os


path_excell = 'C:\\Users\jainil\Documents\\book1.xlsx'
excel = win32com.client.Dispatch("Excel.Application")
if os.path.isfile(path_excell):
    wb = excel.Workbooks.Open(path_excell)
else:
    wb = excel.Workbooks.add
sheet = wb.ActiveSheet
new_str = '1|2|3|4|5|6|7|8|9'
new_str = new_str.split('|')
k = 0
i = 1

while k<8400:
    s = 'v'*k
    new_str[7] = s
    sheet.Range(sheet.Cells(i, 1), sheet.Cells(i, 9)).Value = new_str
    k += 1
    i += 1
if os.path.isfile(path_excell):
    wb.Save()
else:
    wb.SaveAs(path_excell)
wb.Close()
excel.Quit()
导入win32com.client
导入操作系统
path_excell='C:\\Users\jainil\Documents\\book1.xlsx'
excel=win32com.client.Dispatch(“excel.Application”)
如果os.path.isfile(path_excell):
wb=excel.Workbooks.Open(路径\u excel)
其他:
wb=excel.Workbooks.add
sheet=wb.ActiveSheet
新|街='1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9'
new_str=new_str.split(“|”)
k=0
i=1

虽然您的版本是Microsoft Office-而不是2007,但它只是用于在.xlsx文件中显示输出。我使用office 2016只是为了显示输出,它与python有关吗?是的,现在我明白你的问题了,你的意思是office 2016工作正常,但office 2007不正常,所以我建议你简单地升级!有些用户可能使用较低版本的office,因此我们从2007年开始不使用office Automation创建Excel文件
xlsx
是一个包含XML文件的zip包,这意味着您不需要安装Excel,只需自己创建文件即可。显然,使用
XlsxWriter
openpyxl
这样的库要容易得多。