简体   繁体   English

python在开放式办公室中自动填写导入文本

[英]python to auto fill in import text in open office

导入文本

(character set ,seperator options, and Fields in Apache Open Office) (Apache Open Office 中的字符集、分隔符选项和字段)

The original file I have is a csv file.我的原始文件是一个 csv 文件。 I want to change the character set ,seperator options, and Fields using python code and save it into an excel file.我想使用python代码更改字符集、分隔符选项和字段并将其保存到excel文件中。 Is it possible to auto fill in these options?是否可以自动填写这些选项? Or else what language can I use to automate this action?或者我可以使用什么语言来自动执行此操作? ( I cant do this in excel because excel will delete some of my special character.) (我不能在 excel 中这样做,因为 excel 会删除我的一些特殊字符。)

You can use xlsxwriter module to make XLSX files: https://xlsxwriter.readthedocs.io/index.html您可以使用xlsxwriter模块制作 XLSX 文件: https : //xlsxwriter.readthedocs.io/index.html

Assume you have a CSV file with encoding CP1251 and you want to get XLSX file with encoding UTF8.假设您有一个编码为 CP1251 的 CSV 文件,并且您想要获得编码为 UTF8 的 XLSX 文件。 Here is how it can be done:这是如何做到的:

import xlsxwriter # pip3 install xlsxwriter

# get data from the csv file with non utf8 encoding
with open('data_cp1251.csv', 'r', encoding='cp1251') as f:
    data = f.read()

# convert the data into 2d array
table = [row.split(",") for row in data.split("\n")]

# create xlsx file (utf8 encoding by default)
ss = xlsxwriter.Workbook('data.xlsx')
s = ss.add_worksheet()

# fill the xlsx file with the 2d array
for row_num, row in enumerate(table):
    for col_num, cell in enumerate(row):
        s.write(row_num, col_num, cell)

ss.close() # here you get the 'data.xlsx' file

For simply cases it works even if the source CSV file has tab \\t separators.对于简单的情况,即使源 CSV 文件具有制表符\\t分隔符,它也能工作。 But it's need to test on your real data.但这需要在您的真实数据上进行测试。

And as far as I can tell all fields in the new xlsx file are 'text fields' by default.据我所知,默认情况下,新 xlsx 文件中的所有字段都是“文本字段”。 You can change their formats any time, see here: https://xlsxwriter.readthedocs.io/format.html#format您可以随时更改其格式,请参见此处: https : //xlsxwriter.readthedocs.io/format.html#format

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM